2017-04-01 69 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