php - Laravel 5 relationship of three models -


orders table

 id   total        created_at  ---+------------+--------------  2    1500.99      2017-02-02 

items table

 id   order_id    product_id   quant  ---+-----------+------------+-------  66    2              5          1  67    2              6          2  68    3              5          2 

products table

 id   name        created_at  ---+------------+--------------  5   pc intel    2017-02-02  6   pc amd      2017-02-02 

models

//order model class order extends model{  public function items(){     return $this->hasmany('app\item');  } }  // item model class item extends model{  public function producto(){     return $this->hasone('app\product'); } }   // product model class product extends model{  public function item(){     return $this->hasone('app\item');  } } 

i want display product name of relationhip models. example have

 $order = order::with('items')->find($id); 

but how can add third model print name of product?? example print this

$order->items->product->name //"pc intel" 

$order->items returns collection. cannot access properties.

instead, have loop through objects in collection

foreach($order->items $item) {     $item->product->name; } 

perhaps should add products eager loading

order = order::with('items.producto')->find($id); 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -