从 Laravel 4 中的多对多模型中删除关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18731653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:24:41  来源:igfitidea点击:

Remove reationship from many-to-many models in laravel 4

phplaraveleloquent

提问by localhost

I tried the following:

我尝试了以下方法:

$one = OneModel::findOrFail($id);
$two = $one->two_model()->findOrFail($two_id);
$two->delete();

But that deletes the record from the database, how can I just remove the relationship without deleting from the table? And also not having to mess with the pivot table, because if that is needed, why am I even using a framework...

但这会从数据库中删除记录,我如何才能删除关系而不从表中删除?而且也不必弄乱数据透视表,因为如果需要,为什么我什至使用框架......

回答by Arda

If I got you correctly, detach()is what you're looking for:

如果我猜对了,detach()就是你要找的:

$one = OneModel::findOrFail($id);
$one->two_model()->detach($two_id);

This will delete only the relation with one_model's table's $idand two_model's table's $two_idin your pivot table.

这将仅删除数据透视表中与one_model's table's$idtwo_model's table's的关系$two_id

Click here for more details.

单击此处了解更多详情