Ich möchte eine Admin-Seite zum Löschen von Bildern erstellen.Laravel: Umgang mit Abhängigkeit beim Löschen aus der Datenbank
In admin.blade.php
:
@if (Auth::check())
@foreach ($images as $image)
<img height='100px' width='100px' src="storage/images/{{$image->file_path}}" alt="Image {{ $image->file_path }}"></p>
<form action="image/{{$image->id}}/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', '[email protected]');
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');
}
}
Auf den Delete image
-Schaltfläche klicken, erhalte ich diese langwierige Fehlermeldung:
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)
Warum passiert das?
EDIT: Migrationen für Bilder und Beschriftungen, wie in den Kommentaren gefordert.
Von 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');
}
}
Von 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');
}
}
Wie der Fehler vermuten lässt, haben Sie eine Abhängigkeit (wahrscheinlich 'captions') für das' image'. Sie müssen auch die zugehörige Überschrift löschen. Zeigen Sie uns einfach die 'Migrationen' für Bilder und Bildunterschriften. – linuxartisan
'Migrationen' wurden hinzugefügt. Es gibt auch 'Likes', willst du sie auch sehen? Die 'Likes'-Tabelle enthält eine ID der 'Caption', auf die sie sich bezieht. – Sahand