Laravel - 多对多

Est*_*ern 4 php database many-to-many relationship laravel

我有两个具有多对多关系的主表和一个数据透视表。

表格

型号产品

public function orders()
{
    return $this->belongsToMany('App\Order');
}
Run Code Online (Sandbox Code Playgroud)

型号订购

public function products()
{
    return $this->belongsToMany('App\Product', 'OrderDetails');
}
Run Code Online (Sandbox Code Playgroud)

$orders = Equipment::all();

@foreach($orders as $order)
    @foreach($order->products as $product)
        {!! $product->name !!} //it works
        // How to print the value of the quantity?
    @endforeach
@endforeach
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能打印数量的值?

Ale*_*nin 5

尝试->pivot->quantity

{!! $product->pivot->quantity !!}
Run Code Online (Sandbox Code Playgroud)

另外,添加withPivot一个关系:

return $this->belongsToMany('App\Product', 'OrderDetails')->withPivot('quantity');
Run Code Online (Sandbox Code Playgroud)

https://laravel.com/docs/5.2/eloquent-relationships#many-to-many