Laravel - 多对多

Laravel - Many To Many

我有两个主要 table 具有多对多关系和一个枢轴 table。

Tables

型号产品

public function orders()
{
    return $this->belongsToMany('App\Order');
}

模特订单

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

$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

如何打印数量的值?

尝试 ->pivot->quantity:

{!! $product->pivot->quantity !!}

此外,将 withPivot 添加到关系中:

return $this->belongsToMany('App\Product', 'OrderDetails')->withPivot('quantity');

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