php - Laravel:从数据库中删除时如何处理依赖关系

标签 php mysql sql 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', 'ImageController@destroy');

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');
    }
}

单击删除图像按钮后,我收到了这条冗长的错误消息:

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');
    }
}

最佳答案

您收到错误是因为其他表中的图像存在外键(例如 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
    ...

另外,您在评论中提到您有一个“likes表,其外键为images”。对喜欢的迁移执行相同的操作。

最后,回滚您的迁移并再次运行它们。这应该创建新表。然后尝试添加和删除图像。

关于php - Laravel:从数据库中删除时如何处理依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43152672/

相关文章:

java - 将java日期转换为Sql时间戳

php - 以编程方式创建类别和多个子类别

php - 倒计时10分钟,运行一个.php文件,然后重启

mysql - SQL - 如果列条目等于 NULL,则将其设置为 0?

java - 运行程序时得到 0>=0

sql - 如何查看复制快照代理状态?

php - 使用 MySQL 条目将按钮连接到 JavaScript 函数

php - SMTP 连接失败并激活 TLS

PHP MySQL - 计算用户发表了多少篇文章

mysql - 如何将变量设置为触发器 MYSQL 中存储过程的结果?