0
I’m developing a system that has an N:N relationship situation and need to create a third table. The relationship is between Product and Coloured. inside the Product Model, I put this function:
public function colors()
{
return $this->belongsToMany('App\Color', 'product_colors', 'product_id', 'color_id')
->withPivot('price', 'id');
}
Now I needed to access a middle element of the table without giving a foreach. How should I do this query?
is this what you seek ?
$product->colors()->findOrFail(1, ['product_color_id']);
– 13dev
foreach ($product->Colors() as $color) { echo $color->pivot->price; }
– Jorge Costa
I wish I didn’t have to use the foreach Jorge Costa.
– Nego Thales
findOrFail will not function pq it will not fetch from the intermediate table, but from the linked table
– Nego Thales