2016-05-08 41 views
4

私は数多くの関係を持つ2つのメインテーブルとピボットテーブルを持っています。Laravel - Many To Many

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 

私は量の値を印刷するにはどうすればよいですか?

+0

あなたが使用することができ、いくつかの異なるアプローチがあります。私は一般にorder_itemsという名前のテーブルを使用し、orderテーブルでは、hasManyをoder_itemsに、d HasManyThroughをproductテーブルと使用します。あなたのケースでは、ピボットテーブル、あなたはwithPivot関数を使用することができます。この記事では、[ピボットテーブル](http://webnifier.com/pivot-table-many-to-many-relationships/) – Ankit

答えて

関連する問題