Laravel - many for many

I have two main tables with many relationships and a pivot table.

Tables

Product model

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

Model order

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

and

$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

What should I do to print the quantity value?

+4
source share
1 answer

Try ->pivot->quantity:

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

Also add withPivotto rationalization:

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

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

+4
source

All Articles