0

Laravel in many to many sync section has an example:

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

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

lets say we have this array retrived from user selected roles:

[2,3,1 => ['expires' => true]]

but this array will not remain like this.in fact both 3 and 1=> .. has the array key equivalent to 1 .so the 3 will be overridden .at the end this array will be turned to :

 [2,1 => ['expires' => true]] //array value 3 is removed 

how may i solve this issue as i have no control over roles ids order?

alex
  • 7,551
  • 13
  • 48
  • 80

1 Answers1

0

Use syncWithoutDetaching or sync($ids, false) for your issue https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php#L70

Vuong
  • 617
  • 11
  • 26