0

This is the error I am thrown when I try to delete my categories entry with products under that parent category:

Illuminate \ Database \ QueryException
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent     row: a foreign key constraint fails (`store`.`products`, CONSTRAINT     `products_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))    (SQL: delete from `categories` where `id` = 1)

After I did some research I do know that you cannot delete a parent with existing children

I am not sure how to join the products with my category id when I delete the category id. This way I can delete all products with the associated category id.

Here is my function for deleting:

public function postDestroy() {
    $category = Category::find(Input::get('id'));

    if ($category) {
        $category->delete();
        return Redirect::to('admin/categories/index')
            ->with('message', 'Category Deleted');
    }

    return Redirect::to('admin/categories/index')
        ->with('message', 'Something went wrong, please try again');

} 
user3721831
  • 11
  • 1
  • 4
  • Possible duplicate [Cannot delete or update a parent row: a foreign key constraint fails](http://stackoverflow.com/questions/1905470/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails) – Ambrish Jun 11 '14 at 05:09
  • @Ambrish... not really, this is a question about Laravel, although it is a related question, it's not asking _why_ the query fails, but rather, how to fix it "Laravel style" – Tim Groeneveld Jun 11 '14 at 07:01

1 Answers1

3

If you want to delete any products that have the same category, I would change your category class that extends eloquent to something like this:

class Category extends Eloquent
{
    // ... all your existing code...
    public function delete()
    {
        // Delete all of the products that have the same ids...
        Products::where("category_id", $this->id)->delete();

        // Finally, delete this category...
        return parent::delete();
    }
}

Now calling $category->delete() will delete all the products that have the same category_id, as well as the category itself.

Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
  • Thanks @timgws. [Here](http://laravel.io/bin/Qma0v) is my original controller. [Here](http://laravel.io/bin/Pk8qv) is the updated controller where I added what you suggested and I still get a error. – user3721831 Jun 11 '14 at 07:11
  • If I change extends Basecontroller to Eloquent I get errors. – user3721831 Jun 11 '14 at 07:15
  • Yes, because your editing your controller. You don't need to edit your controller, edit your _model_. Change the Categories model (that should extend Eloquent) in `app/models`. That way you get to keep `$category->delete` and the rest of your controller unchanged. In the future, when you delete a category, it will delete the associated products with it! – Tim Groeneveld Jun 11 '14 at 07:18
  • I have another question to post that has to do with updating the products on the same page they are created. If I cannot figure it out I will come back to this post and post the question link if you do not mind. – user3721831 Jun 11 '14 at 07:29