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');
}