thouseer
thouseer

Reputation: 25

Laravel Eloquent 4 table relationship

enter image description here

my database table structure

this my purchase model

class Purchase extends Model
{
   public function supplierDetails(){
        return $this->belongsTo('App\Supplier', 'supplier_id');
    }
    public function purchasedItems(){
        return $this->hasMany('App\PurchasedItem', 'purchase_id');
    }
}

this my controller method

 public function getReport(){
        $result =  Purchase::with('supplierDetails', 'purchasedItems')->get();
        return $result;
    }

this is my json result

[
    {
        "id": 74,
        "supplier_id": 3,
        "invoice_no": "wds2",
        "supplier_details": {
            "id": 3,
            "name": "alexy" 
        },
        "purchased_items": [
            {
                "id": 114,
                "purchase_id": 74,
                "item_id": 2
            },
            {
                "id": 115,
                "purchase_id": 74,
                "item_id": 3
            }
        ]

    }
]

i can make relationship between these Three models 'Supplier', 'Purchase', and 'PurchaseItem' but cannot make relationship with 'Item' models

i want json response like this

[
    {
        "id": 74,
        "supplier_id": 3,
        "invoice_no": "wds2",
        "supplier_details": {
            "id": 3,
            "name": "alexy" 
        },
        "purchased_items": [
            {
                "id": 114,
                "purchase_id": 74,
                "item_id": 2,
                "item_details": {
                    "id": 2,
                    "name": "item1" 
                }
            },
            {
                "id": 115,
                "purchase_id": 74,
                "item_id": 3,
                "item_details": {
                    "id": 3,
                    "name": "item2" 
                }
            }
        ]

    }
]

Any idea?

Upvotes: 2

Views: 516

Answers (2)

thouseer
thouseer

Reputation: 25

i made some changes and the below code works..

 public function getAll(){
        $array =  Purchase::with('supplierDetails', 'purchasedItems')->get();
        $purchases = json_decode($array,true);

        $result = array();
        foreach($purchases as $p){
            $itemsPurchased = $p['purchased_items'];

            foreach($itemsPurchased as $pitem){
                $pitem['item_details'] = PurchasedItem::with('itemDetails', 'packageDetails')->where('purchase_id', '=', $pitem['purchase_id'])->get();
            }
            $p['supplier_details'] = $p['supplier_details'];
            $p['purchased_items'] = $pitem['item_details'];

            array_push($result, array($p));
        }
        return $result;
        //[0] added to remove []sqauare bracket
    }

Upvotes: 0

Amir
Amir

Reputation: 721

This might help, i haven't tested but you should and let me know.

 public function getReport(){
    $purchases = Purchase::all;
    $result;

    foreach($purchases as $p){

      $itemsPurchased = $p->purchasedItems;
        foreach($itemsPurchased as $pitem){
            $pitem['item_details'] = $pitem->YOUR.FUNCTION.NAME.FOR.ITEM.DETAILS.
        }

      $p['purchased_items'] = $itemsPurchased;
      $p['supplier_details'] = $p->supplierDetails;
      $result = array($p);

    }

   return $result;

}

Upvotes: 2

Related Questions