2017-04-01 64 views
1

刪除我想刪除的圖像創建一個管理頁面時,處理的依賴性。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'); 
    } 
} 
+0

由於錯誤提示,你已經得到了'image'的依賴(可能是'captions')。您也需要刪除相關的「標題」。只需向我們展示圖像和標題的「遷移」。 – linuxartisan

+0

'migrations'已被添加。還有'喜歡',你也想看看嗎? 「喜歡」表包含與其相關的「標題」的ID。 – Sahand

回答

0

因爲有在其他表(例如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圖像0​​likes`遷移的評論提及。

最後,回滾您的遷移和重新運行它們。這應該創建新的表格。然後嘗試添加和刪除圖像。

+0

感謝您的回覆。 '喜歡'包含一行'caption_id',但它不是外鍵。我應該因此忽略「喜歡」嗎? – Sahand

+0

是的,無視它。試試看看你的錯誤是否消失。如果有,請接受答案。 – linuxartisan

+0

@Sandi嘿,解決問題有什麼好運? – linuxartisan