I have one to many relationship
In my Task controller
public function todo()
{
return $this->belongsTo('App\Todo');
}
In my Todo controller
public function tasks()
{
return $this->hasMany('App\Task');
}
Adding the relationship with the following code
$todo = new Todo
$todo->save();
$task = new Task
$todo->tasks()->save($task);
$task->save();
but i want to delete it later not the object just the relationship
any ideas
In this chapter of the docs:
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();
So in your case,
$task->todo()->dissociate();
$task->save();
Assuming a normal schema, you'll have a todo_id
column on your tasks
table. Unset it for the record you want to detach.
$task->todo_id = null;
$task->save();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With