从 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
Remove reationship from many-to-many models in laravel 4
提问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 $id
and two_model
's table's $two_id
in your pivot table.
这将仅删除数据透视表中与one_model
's table's$id
和two_model
's table's的关系$two_id
。