7

Here is the thing, I have 3 tables, users / users_types / types.

I have a belongToMany between users and types through users_types, the pivot with a few infos in it. One of them being the line number in a form. I am trying to update the table based on the userID and the line number but NOT on the typeID, the latter being filled by my inputs.

How can I make it happen? I have been trying

updateExistantPivot($line_number->line_number,array(
                                                        'type_id'  => $type_id,
                                                        'etc'             => $etc,
                                                        'duration'          => $duration
                                                    )
                                                );

But obviously it will not worked since it wants the typeID instead of the line_number... I always want to update the same line_number & userID. (I am in a for loop for every lines).

Thanks for your help!

oh, and I did try with a sync... but it gives me a foreign key error because it sends numbers that are not supposed to be there.

commandantp
  • 947
  • 1
  • 12
  • 25

2 Answers2

11

If you want to update existing pivot, you can do this:

$model; // parent of the relation
$related; // related object already synced with the $model

$model->relation()->sync([$related->id => [ 'duration' => 'someValue'] ], false);

1st param is array with related model id as key, and array of pivot values to update, while 2nd param set to false means, that you don't detach all the other related models.

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • Hey @JarekTkaczyk OMG! Thank you so much !!! It worked but I had to change the false to true in order to update and not re-create a new line everytime! The documentations from Laravel is not explaining as good ! Thanks again! – commandantp Sep 26 '14 at 17:53
  • 1
    Not really - when you use `sync` then nothing is duplicated, but rather updated. In case you used `attach`, then yes, you would create new pivot entry evry time. Mind that not using `false` there resets all the pivot entries for given model, and add only those provided. – Jarek Tkaczyk Sep 26 '14 at 18:32
  • Yes I did use attach to create that entry - is that why then I had to use the false? Ummmm... Does it matter if the pivot is reset since the datas go in the table? I did notice when I was doing things wrong that my simple sync was deleting the whole entry when there was an error and the code stopped. Still confused on how the pivot works to be honest... I understood that it was an temporary array that created with the infos all the linked tables + the ones from the linking table (which you define)...is it correct? Thanks! – commandantp Sep 26 '14 at 20:23
  • @Jarek Tkaczyk, Seems you can help me. Look at this : https://stackoverflow.com/questions/44519339/how-can-i-update-pivot-table-on-laravel – moses toh Jun 13 '17 at 10:52
0

Also now with Laravel 7 you can use

syncWithoutDetaching

Link in documentation https://laravel.com/docs/7.x/eloquent-relationships#many-to-many

Menawer
  • 843
  • 4
  • 12