sge*_*ger 5 php laravel eloquent
我有一对多的关系
在我的任务控制器中
public function todo()
{
return $this->belongsTo('App\Todo');
}
Run Code Online (Sandbox Code Playgroud)
在我的 Todo 控制器中
public function tasks()
{
return $this->hasMany('App\Task');
}
Run Code Online (Sandbox Code Playgroud)
用以下代码添加关系
$todo = new Todo
$todo->save();
$task = new Task
$todo->tasks()->save($task);
$task->save();
Run Code Online (Sandbox Code Playgroud)
但我想稍后删除它而不是对象只是关系
有任何想法吗
tho*_*ulb 10
在文档的本章中:
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();
Run Code Online (Sandbox Code Playgroud)
所以就你而言,
$task->todo()->dissociate();
$task->save();
Run Code Online (Sandbox Code Playgroud)
todo_id假设有一个正常的架构,您的表上将有一列tasks。为您要分离的记录取消设置。
$task->todo_id = null;
$task->save();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6753 次 |
| 最近记录: |