Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel CRUD delete不处理外键_Php_Mysql_Laravel_Laravel 5.2 - Fatal编程技术网

Php Laravel CRUD delete不处理外键

Php Laravel CRUD delete不处理外键,php,mysql,laravel,laravel-5.2,Php,Mysql,Laravel,Laravel 5.2,我与数据库中的category_id列存在外部关系,但在删除时出现错误。 这是我的删除代码: public function destroy($id) { $category = Category::find($id); $category->delete(); Session::flash('success', 'The category was successfully deleted.'); return redirect()->route(

我与数据库中的category_id列存在外部关系,但在删除时出现错误。 这是我的删除代码:

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

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)

请帮助。

我的赌注是外键被设置为on DELETE RESTRICT而不是CASCADE

无法删除或更新父行:外键约束失败(
fitilicious
products
,约束
products\u category\u id\u foreign
外键(
category\u id
)引用
categories
id
)(SQL:delete from
categories
其中
id
=2)

这说明表“products”中有一行引用了您试图删除的类别

  • 在删除时,CASCADE也会删除产品
  • ON DELETE RESTRICT将阻止删除非空类别
  • 在DELETE上,SET NULL将删除类别,并将products表中的category_id设置为NULL

每个都有其用途,但您需要选择所需的用途。

我的赌注是将外键设置为on DELETE RESTRICT而不是CASCADE

无法删除或更新父行:外键约束失败(
fitilicious
products
,约束
products\u category\u id\u foreign
外键(
category\u id
)引用
categories
id
)(SQL:delete from
categories
其中
id
=2)

这说明表“products”中有一行引用了您试图删除的类别

  • 在删除时,CASCADE也会删除产品
  • ON DELETE RESTRICT将阻止删除非空类别
  • 在DELETE上,SET NULL将删除类别,并将products表中的category_id设置为NULL

每个都有它的用途,但是你需要选择你需要的。

但是我们在laravel delete中编写代码来实现这一点,但是我们在laravel delete中编写代码来实现这一点呢