Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravels syncWithoutDetaching and additional data

I have Googled my fingers sore, and I can't see anyone discussing this, but I have a suspicion that Laravels syncWithoutDetaching() method doesn't take any parameters for extra data like save(), sync() and attach() does?

Does anybody know this? In the API documentation the method has the following parameters:

array syncWithoutDetaching(Collection|Model|array $ids)

I have trouble adding existing data to a relationship between a Guest and an Event. I need to add status for the guests and what event they are attending, maybe attending or declined.

like image 425
rebellion Avatar asked Mar 01 '19 11:03

rebellion


1 Answers

sync() and syncWithoutDetaching() both don't have a parameter for additional values, you have to pass the additional values as an array with the ids.


According to the docs:

You may also pass additional intermediate table values with the IDs:

$user->roles()->sync([
    1 => ['expires' => true],
    2,
    3
]);


If you look here you can see that syncWithoutDetaching() just calls sync() but passes false as the second argument.

In your case it would be something like this:

$event->guests()->syncWithoutDetaching([
    1 => ['attending' => true],
    2 => ['attending' => false]
])
like image 114
Remul Avatar answered Sep 28 '22 06:09

Remul