简体   繁体   English

无法删除或更新 Laravel 中的项目

[英]can't delete or update item in laravel

when i tried delete item in laravel i get this message当我尝试在 Laravel 中删除项目时,我收到此消息

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails ( larblog . comments , CONSTRAINT comments_article_id_foreign FOREIGN KEY ( article_id ) REFERENCES articles ( id )) (SQL: delete from articles where id = 2) SQLSTATE [23000]:完整性约束违规:1451无法删除或更新父行,外键约束失败( larblogcomments ,约束comments_article_id_foreign外键( article_id )参考articlesid ))(SQL:从删除articles ,其中id = 2)

this my delete function这是我的删除功能

 public function DeleteArticle($id){
        $article = Article::find($id);
        $article->delete();
        return redirect("article");
    }

this create articles table code这个创建文章表代码

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticle extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

and this create comments table code这创建评论表代码

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateComments extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->text('comment');
            $table->timestamps();
            $table->integer('article_id')->unsigned();
            $table->foreign('article_id')->references('id')->on('articles');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
}

Iam tried to use this solution but did not work我尝试使用此解决方案但没有奏效

$table->foreign('article_id')->references('id')->on('articles')->onUpdate('cascade')- 
>onDelete('cascade');

as you know your comments depends on articles once a article is delete then comment's relation going to break so either you first delete all comment's of that artical or set foreign key null to all the comment for that article如您所知,一旦文章被删除,您的评论取决于文章,然后评论的关系就会中断,因此您要么先删除该文章的所有评论,要么将外键设置为该文章的所有评论为空
so that why you need to update your mygration with所以这就是为什么你需要更新你的 mygration

$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');

or或者

$table->integer('article_id')->unsigned()->nullable();

$table->foreign('article_id')->references('id')->on('articles')->onDelete('set null');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM