Search code examples
phplaravellaravel-5

sync() and attach() not working together?


In my controller first i attach array of ids like this for my additional fields:

$property->features()->attach($additional_ids);

Then im using sync() because i have checkboxes :

 $property->features()->sync($features);

But problem is when i have this then my attach() is not working. Any suggestion how can i fix this? When i remove this sync() my attach is working. So idea is that i have checkboxes for features, and i have input fields for some additional features.I store this ids in pivot table .


Solution

  • sync() will override all changes made by attach(). So, just merge arrays and use sync():

    $property->features()->sync(array_merge($features, $additional_ids));