【问题标题】:From campaign table i want to delete campaign but i can't delete..following error occur从广告系列表中我想删除广告系列,但我无法删除..出现以下错误
【发布时间】:2018-03-20 09:56:19
【问题描述】:

SQLSTATE[23000]:违反完整性约束:1451 无法删除或 更新父行:外键约束失败 (demo purpose_fundraising.campaign_product,约束 campaign_product_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES 活动 (id))(SQL:从 id = 60 的活动中删除)

广告系列表架构:

Schema::create('campaign', function (Blueprint $table) {
            $table->engine='InnoDB';
            $table->increments('id');
            $table->integer('users_id')->unsigned();
            $table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $table->string('campaign_name');
            $table->float('campaign_goal',8,2);
            $table->string('discription',400);
            $table->string('image');
            $table->string('category');
            $table->date('start_date');
            $table->date('end_date');
            $table->float('total_fund',8,2);
});

campaign_product 表架构:

Schema::create('campaign_product', function (Blueprint $table) {
            $table->engine='InnoDB';

            $table->increments('id');
            $table->integer('campaign_id')->unsigned();
            $table->foreign('campaign_id')->references('id')->on('campaign')->onDelete('cascade')->onUpdate('cascade');

            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('product')->onDelete('cascade')->onUpdate('cascade');
        });

campaign_id 是 Campaign_product 删除中的外键。

我想删除广告系列..
如何删除广告系列产品然后广告系列??

【问题讨论】:

标签: php mysql database laravel


【解决方案1】:

除非您从campaign_product 表中删除那些campaign_id,否则您无法删除它。在删除营销活动之前,从 campaign_product 表中分离产品。示例:

$campaign = Campaign::query()->findOrFail($id); //find campaign
$campaign->products()->detach($campaign->product); //detach products from `campaign_products` table
$campaign->delete(); //delete the campaign

阅读有关分离多对多关系记录的更多信息:https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships

【讨论】:

  • 什么是$id? $id 的值是多少?
  • $id 是您要删除的行 ID。顺便问一下,您如何删除记录?
  • 我得到了我想在 $campaign 中删除的特定广告系列的所有详细信息。如何获取该广告系列的 id.. 发生未定义 $id 错误.......公共函数销毁(活动 $campaign) { $campaign = Campaign::query()->findOrFail($id); $campaign->products()->detach($campaign->product); $campaign->删除(); return Redirect::route('campaign.index')->with('campaign删除成功'); }
  • destroy()方法中传递$id,如destroy($id),无需绑定Campaign模型。
  • 我照你说的做.. 但是现在出现 Method products does not exist 错误..
【解决方案2】:

您看到的是您无法删除模型,因为它在您的数据库中仍然有连接。您想创建一个函数,在删除相关模型之前先删除所有关系。

尝试将此Campaign 模型添加给您。

protected static function boot() {
   parent::boot();

   static::deleting(function($campaign) { // before delete() method, call this
       $campaign->products()->delete();
       // do the rest of the cleanup...
   });
}

如果您按照 cmets 中链接中给出的流程进行操作,您可以防止此问题在未来发生。

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多