Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Eloquent relation sync also remove?

When updating a model and I sync a relationship, if I don't pass in all the ids that already exist, will that relationship be removed?

like image 219
Jason Spick Avatar asked Jun 13 '14 14:06

Jason Spick


1 Answers

You decide: sync has 2nd parameter that defaults to true and is responsible for detaching:

$model->relationship()->sync([1,2,3]);

$model->relationship()->sync([4,5,6]); // attached [4,5,6], detached [1,2,3]
$model->relationship()->getRelatedIds(); // [4,5,6]

// but:
$model->relationship()->sync([4,5,6], false); // attached [4,5,6], detached []
$model->relationship()->getRelatedIds(); // [1,2,3,4,5,6]
like image 177
Jarek Tkaczyk Avatar answered Oct 16 '22 08:10

Jarek Tkaczyk