update pivot table in case of many to many relation laravel4 update pivot table in case of many to many relation laravel4 laravel laravel

update pivot table in case of many to many relation laravel4


Old question, but on Nov 13, 2013, the updateExistingPivot method was made public for many to many relationships. This isn't in the official documentation yet.

public void updateExistingPivot(mixed $id, array $attributes, bool $touch)

--Updates an existing pivot record on the table.

As of Feb 21, 2014 you must include all three arguments.

In your case, (if you wanted to update the pivot field 'foo') you could do:

$product->tags()->updateExistingPivot($tag->tagID, array('foo' => 'value'), false);

Or you can change the last boolean false to true if you want to touch the parent timestamp.

Pull request:

https://github.com/laravel/framework/pull/2711/files


Another method for this while working with laravel 5.0+

$tag = $product->tags()->find($tag_id);$tag->pivot->foo = "some value";$tag->pivot->save();


I know that this is an old question, but if you're still interested in the solution, here it is:

Lets say your pivot table has 'foo' and 'bar' as the additional attributes, you can do this to insert data into that table:

$product->tags()->attach($tag->tagID, array('foo' => 'some_value', 'bar'=>'some_other_value'));