0

Im using Laravel and Eloquent to relate two models.

Fairly simple, a webshop has many brands, but I have some extra information stored in the relation table that I need.

How do I get this?

My relation table looks like this:

brand_id   |    webshop_id    |   url

My current statement to get the webshops including the brands is:

$webshop = Webshop::select()
        ->where('slug', $slug)
        ->with('brands')
        ->first();

Then I can look through $webshop->brands but the column "url" is not accesable.

Christoffer
  • 482
  • 7
  • 32
  • 2
    See this: https://stackoverflow.com/questions/26566675/getting-the-value-of-an-extra-pivot-table-column-laravel – Laraleg Nov 17 '17 at 10:46

1 Answers1

3

On your Webshop model:

public function brands()
{
    return $this->belongsToMany('App\Brand')->withPivot('url');
}

Then just do

foreach ($webshop->brands as $brand)
{
    echo $brand->pivot->url; // your url
}
tompec
  • 1,220
  • 9
  • 16