刪除我想刪除的圖像創建一個管理頁面時,處理的依賴性。Laravel:如何從數據庫
在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', '[email protected]');
在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');
}
}
在點擊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 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');
}
}
從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');
}
}
由於錯誤提示,你已經得到了'image'的依賴(可能是'captions')。您也需要刪除相關的「標題」。只需向我們展示圖像和標題的「遷移」。 – linuxartisan
'migrations'已被添加。還有'喜歡',你也想看看嗎? 「喜歡」表包含與其相關的「標題」的ID。 – Sahand