Thursday, March 30, 2017

Laravel CRUD delete not working on foreign key

I have a foreign relationship with category_id column in database but while deleting i get error. This is my code for delete:

  public function destroy($id)
{
    $category = Category::find($id);
    $category->delete();
    Session::flash('success', 'The category was successfully deleted.');
    return redirect()->route('categories.index');
}

Error i seen is :

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

Please help.



via Sultan jahan

Advertisement