0

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (demopurpose_fundraising.campaign_product, CONSTRAINT campaign_product_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign (id)) (SQL: delete from campaign where id = 60)

campaign table schema :

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 table schema :

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 is foreign key in campaign_product delete..

I want to delete campaign ..
How to delete campaign product then campaign ??

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Unnati Patadia
  • 662
  • 3
  • 19
  • 39
  • 3
    Possible duplicate of [Laravel Cannot delete or update a parent row: a foreign key constraint fails](https://stackoverflow.com/questions/47544109/laravel-cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails) – Thamaraiselvam Mar 20 '18 at 10:00
  • Its not solve my problem..@Thamaraiselvam – Unnati Patadia Mar 20 '18 at 10:12
  • How are you trying to delete the record? paste some code... – Calin Blaga Mar 20 '18 at 10:22
  • maybe this will help https://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm/27805553#27805553 Delete campaign_product, then campaign – Calin Blaga Mar 20 '18 at 10:25

2 Answers2

1

You cannot delete it unless you remove those campaign_id from campaign_product table. Detach products from the campaign_product table before you delete a campaign. Example:

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

Read more about detaching many-to-many relationship record: https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships

smartrahat
  • 5,381
  • 6
  • 47
  • 68
  • What is $id? what is the value in $id? – Unnati Patadia Mar 21 '18 at 05:59
  • `$id` is the row id you want to delete. How do you delete a record, by the way? – smartrahat Mar 21 '18 at 07:14
  • I get all detail of that particular campaign which i want to delete in $campaign.. How to fetch id of that campaign.. Undefine $id error occur.......public function destroy(Campaign $campaign) { $campaign = Campaign::query()->findOrFail($id); $campaign->products()->detach($campaign->product); $campaign->delete(); return Redirect::route('campaign.index')->with('campaign deleted successfully.'); } – Unnati Patadia Mar 21 '18 at 07:18
  • Pass `$id` inside `destroy()` method like `destroy($id)`, no need to bind the Campaign model. – smartrahat Mar 21 '18 at 07:21
  • i do what u say .. but now Method products does not exist error occur.. – Unnati Patadia Mar 21 '18 at 07:28
0

What you are seeing is you cant delete a model because it still has connection in your database. You want to make a function that first deletes all the relations before deleting the model in question.

Try adding this to you Campaign Model.

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

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

If you follow the process given in the link in the comments you can prevent this issue from happening in the future.

Taacoo
  • 277
  • 1
  • 3
  • 11