我有一对多关系
在我的任务 Controller 中
public function todo()
{
return $this->belongsTo('App\Todo');
}
在我的 Todo Controller 中
public function tasks()
{
return $this->hasMany('App\Task');
}
使用以下代码添加关系
$todo = new Todo
$todo->save();
$task = new Task
$todo->tasks()->save($task);
$task->save();
但我想稍后删除它,而不是对象,只是关系
任何想法
最佳答案
When removing a belongsTo relationship, you may use the dissociate method.
This method will reset the foreign key as well as the relation on the child model:
$user->account()->dissociate();
$user->save();
所以就你而言,
$task->todo()->dissociate();
$task->save();
关于php - 删除与 Laravel 5 的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31589709/