I have started working with Laravel4 recently. I am facing some problem while updating pivot table data, in case of many to many relation.

The situation is: I have two table: ProductProductType. The relation between them is Many to many. My Models are

class Product extends Eloquent {
    protected $table = 'products';
    protected $primaryKey = 'prd_id';

    public function tags() {
        return $this->belongsToMany('Tag', 'prd_tags', 'prta_prd_id', 'prta_tag_id');
    }
}

class Tag extends Eloquent {
    protected $table = 'tags';
    protected $primaryKey = 'tag_id';
        public function products()
    {
    return $this->belongsToMany('Product', 'prd_tags', 'prta_prd_id', 'prta_tag_id');
    }
}

While inserting data to the pivot table prd_tags, I did:

$product->tags()->attach($tag->tagID);

But now I want to update data in this pivot table, what is the best way to update data to the pivot table. Let's say, I want to delete some tags and add new tags to a particular product.

shareimprove this question
 
   
Did you read the information in the docs? Does it help at all... four.laravel.com/docs/eloquent#working-with-pivot-tables – Phill Sparks Apr 11 '13 at 12:24
   
@PhillSparks: Yes, I have gone through this document. there is method called sync to do this, which will take an array of id's and insert and delete respectively but what if my pivot table have attributes other than id's of both table. The sync method should take an array of objects instead of array of integers. – Sameer Apr 15 '13 at 7:55
1 
Eloquent promotes a simple table design where your pivot tables have ID columns used to reference the rows. Eloquent was not designed to cater for other database designs, as there are more comprehensive ORM solutions available. – Phill Sparks Apr 15 '13 at 8:36

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

shareimprove this answer
 
2 
Fantastic. I wish the official docs were updated to highlight this method. This should be marked as the correct answer. Cheers! – James Furey Apr 1 '14 at 1:07
   
Perfect, thanks! – Guicara Aug 18 '14 at 9:54
   
public void, typo? – haakym Nov 19 '15 at 10:12
1 
@haakym Not a typo, at the time it didn't return anything. Looks like it now returns an int, presumably the new id. I'll edit the answer. Thanks. – Andrew Nov 19 '15 at 17:07

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'));
shareimprove this answer
 

Another method for this while working with laravel 5.0+

$tag = $product->tags()->find($tag_id);
$tag->pivot->foo = "some value";
$tag->pivot->save();
shareimprove this answer
 
1 
I have to do $pivot->pivot->save(); to save my pivot table. – MECU Apr 22 '16 at 22:19
   
this is actually the easiest solution available I think. – dexterb May 22 at 17:15

this is full example :

 $user = $this->model->find($userId);
    $user->discounts()
        ->wherePivot('discount_id', $discountId)
        ->wherePivot('used_for_type', null)
        ->updateExistingPivot($discountId, [
            'used_for_id' => $usedForId,
            'used_for_type' => $usedForType,
            'used_date_time' => Carbon::now()->toDateString(),
        ], false);
shareimprove this answer
 

来自  https://stackoverflow.com/questions/15621279/update-pivot-table-in-case-of-many-to-many-relation-lar...

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •