Laravel 5 - How to use primary key in pivot table?

0

I have a question that has arisen now. You searched the forums, and I did not find anything like it. I have this schematic in my DB:

users: id name ...

products: id name ...

product_user: id product_id user_id ...

In my User.php, I have:

public function products()
{
    return $this->belongsToMany('App\Product')->withTimestamps();
}

There, in my view, I have an instance of User:     @foreach ($ user- > products as $ product)         // some code here ...     @endforeach

But, I can not get the id of my pivot table, like this:

@foreach($user->products as $product)
    {{ $product->pivot->id }}}
@endforeach

I can even get the other fields, for example: =     {{product-> pivot-> created_at}}}

But the id, I can not. When I give a dd () in $ product-> pivot, and I see the attributes, I have this:

#attributes: array:4 [▼
    "user_id" => 2
    "product_id" => 1
    "created_at" => "2015-03-14 20:25:09"
    "updated_at" => "2015-03-14 20:25:09"
  ]

How do I get this blessed pivot table ID?

    
asked by anonymous 15.03.2015 / 01:08

1 answer

0
public function products()
{
    return $this->belongsToMany('App\Product')->withTimestamps()->withPivot("id");
}
    
18.03.2015 / 00:34