How to get column in Laravel junction table

Mahdi Rezaei :

I implemented Many-To-Many Eloquent relationship between products and properties by three table. (like this)

I can access product properties using the following code :

App\Product::find(1)->properties;

but how to get other column in junction table (such as "value" in this case)?

I have attached my database schema image here

IGP :

Any column that is not a foreign key is not added by default in the relationship object. You need to specify the extra columns you want with withPivot.

# App\Product model
public function properties()
{
    return $this->belongsToMany(...)->withPivot('value');
}

To access the intermediate table, use pivot->

App\Product::find(1)->properties->pivot->value;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13903&siteId=1