【发布时间】:2014-06-11 05:01:30
【问题描述】:
这是我尝试删除包含该父类别下产品的类别条目时引发的错误:
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)
在我做了一些研究之后,我知道你不能删除现有孩子的父母
当我删除类别 ID 时,我不确定如何使用我的类别 ID 加入产品。这样我就可以删除所有具有相关类别 ID 的产品。
这是我的删除功能:
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');
}
【问题讨论】:
-
@Ambrish ... 不是真的,这是一个关于 Laravel 的问题,虽然它是一个相关的问题,但它不是问 why 查询失败,而是问如何修复它“Laravel 风格”