Laravel-在一对多关系中,保存方法的反面是什么


Laravel - What is the opposite of the save method on a one-to-many relationship?

假设我正在对一对多关系进行关联,比如这个

$artwork->views()->save($view);

我该如何删除此关联?

detach()适用于多对多关系,您需要的是dissociate()。根据文件:

$user->account()->dissociate();
$user->save();

https://laravel.com/docs/5.1/eloquent-relationships#inserting-相关型号

您显示的是一个创建关联的代码。我猜你的意思是删除:

App'Views::destroy(245);

https://laravel.com/docs/5.1/eloquent#deleting-型号

扩展@Joel Hinz的答案,只从关系中删除一项:

$artwork->views()->first()->delete();

或者,您可以使用wherelatest等进行选择。