我想创建一个用于删除图像的管理页面。
在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
在routes/web.php
:
//deleting images
Route::post('image/{id}/delete', 'ImageController@destroy');
在App/Http/Controllers/ImageController.php
:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppImage;
class ImageController extends Controller
{
public function destroy($id)
{
$image = Image::findOrFail($id);
$image->delete();
return redirect('admin');
}
}
单击Delete image
按钮后,我收到以下冗长的错误消息:
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)
为什么会这样?
编辑:图像和标题的迁移,如评论中的要求。
从2017_03_10_080608_Create_Image_Table.php
:
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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');
}
}
从2017_03_20_1104_35_create_caption_table.php
:
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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');
}
}
您收到错误,因为其他表中的图像有外键(例如。 captions
(。
只需将onDelete
行为添加到迁移中即可。
Schema::create('captions', function (Blueprint $table) {
$table->increments('id');
$table->integer('image_id')->unsigned();
$table->foreign('image_id')
->references('id')
->on('images')
->onDelete('cascade'); // delete related caption
...
另外,您在评论中提到您有"喜欢table with a foreign key of
图像. Do the same for
喜欢"迁移。
最后,回滚迁移并再次运行它们。这应该会创建新表。然后尝试添加和删除图像。