Friday, March 31, 2017

Laravel: QueryException in Connection.php line 647

I want to create an admin page for deleting images.

In admin.blade.php:

@if (Auth::check())
@foreach ($images as $image)
<img height='100px' width='100px' src="storage/images/" alt="Image "></p>
<form action="image//delete" method="post">
<button type="submit">Delete image</button> 
<br>
@endforeach
@else
<p>Login first</p>
@endif

In routes/web.php:

//deleting images
Route::post('image/{id}/delete', 'ImageController@destroy');

In App/Http/Controllers/ImageController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Image;
class ImageController extends Controller
{
    public function destroy($id)
    {
        $image = Image::findOrFail($id);
        $image->delete();

        return redirect('admin');
    }
}

Upon clicking the Delete image-button, I get this lengthy error message:

QueryException in Connection.php line 647:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`retellgram`.`captions`, CONSTRAINT `captions_image_id_foreign` FOREIGN KEY (`image_id`) REFERENCES `images` (`id`)) (SQL: delete from `images` where `id` = 1)

Why does this happen?

EDIT: Migrations for Images and Captions, as requested in the comments.

From 2017_03_10_080608_Create_Image_Table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateImageTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('file_path');
            $table->string('md5')->index();
            $table->integer('likes')->default(0);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('images');
    }
}

From 2017_03_20_1104_35_create_caption_table.php:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCaptionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('captions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('image_id')->unsigned();
            $table->foreign('image_id')->references('id')->on('images');

            $table->string('content');
            $table->integer('likes')->default(0);
            $table->boolean('approved')->default(false);

            $table->integer('character_id')->unsigned();
            $table->foreign('character_id')->references('id')->on('characters');

            $table->timestamps();

        });

        Schema::create('caption_hashtag', function (Blueprint $table) {
            $table->integer('caption_id')->unsigned()->index();
            $table->foreign('caption_id')->references('id')->on('captions')->onDelete('cascade');

            $table->integer('hashtag_id')->unsigned()->index();
            $table->foreign('hashtag_id')->references('id')->on('hashtags')->onDelete('cascade');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('caption_hashtag');
        Schema::dropIfExists('captions');
    }
}



via Sandi

Advertisement