0
votes

I'm new on this site and also new to Laravel. I have 3 tables.

1) Product Table (column: id, product_name)

2) Purchase Table (Column: id, purchase_date)

3) Purchase Items (Colunn: id, purchase_id, product_id);

Releationships

I have oneToMany Relationship Between Purchase & PurchaseItem Model, and oneToMany relationship between Product & PurchaseItem.

My Problem

In my Purchase Model I'm getting purchaseItems Data easily as I have relationship between them. But I want to get Product Data also (Like: product_name) in Purchase Model. How can I get it? any idea?

1

1 Answers

0
votes

You can just call the relation of your PurchaseItem as you did for Purchase :

 $purchases = Purchase::all();

 foreach ($purchases as $purchase) {
      // get all items
      $purchaseItems = $purchase->purchaseItems;

      foreach ($purchaseItems as $purchaseItem) {
           // you can $purchaseItem->product
           echo $purchaseItem->product->product_name;
      }
 }