can’t select specific columns on laravel relationship

  eloquent, laravel

I have Item model and a relationship with PriceListDetail model if I load the entire relationship I receive the relationships correctly with this code:

$item = Item::select('itemCode', 'itemName')->where('itemCode', 'PRD-1')->with('price_list_details')->get()

And this is the result:

=> IlluminateDatabaseEloquentCollection {#4869
     all: [
       AppItem {#4307
         itemCode: "PRD-1",
         itemName: "Watch",
         price_list_details: IlluminateDatabaseEloquentCollection {#4943
           all: [
             AppPriceListDetail {#4948
               id: 148,
               item_itemCode: "PRD-1",
               price_list_id: 1,
               headquarter_id: 2,
               measurementunit_id: 1,
               percentage: "15.00",
               base_price: 4600.0,
               calculated_value: 5290.0,
               manual: 0,
               created_at: "2021-05-24 12:04:38",
               updated_at: "2021-05-24 12:04:38",
             },
             AppPriceListDetail {#4870
               id: 149,
               item_itemCode: "PRD-1",
               price_list_id: 1,
               headquarter_id: 2,
               measurementunit_id: 2,
               percentage: "15.00",
               base_price: 4600.0,
               calculated_value: 5290000.0,
               manual: 0,
               created_at: "2021-05-24 12:04:38",
               updated_at: "2021-05-24 12:04:38",
             },
             AppPriceListDetail {#4934
               id: 150,
               item_itemCode: "PRD-1",
               price_list_id: 1,
               headquarter_id: 2,
               measurementunit_id: 3,
               percentage: "15.00",
               base_price: 4600.0,
               calculated_value: 264500000.0,
               manual: 0,
               created_at: "2021-05-24 12:04:38",
               updated_at: "2021-05-24 12:04:38",
             },
             AppPriceListDetail {#4927
               id: 741,
               item_itemCode: "PRD-1",
               price_list_id: 1,
               headquarter_id: 1,
               measurementunit_id: 1,
               percentage: "15.00",
               base_price: 4600.0,
               calculated_value: 5290.0,
               manual: 0,
               created_at: "2021-06-04 15:52:39",
               updated_at: "2021-06-04 15:52:39",
             },
           ],
         },
       },
     ],
   }

However if I only want to get the id of each relationship using this query:

$item = Item::select('itemCode', 'itemName')->where('itemCode', 'PRD-1')->with('price_list_details:id')->get();

I receive the relationship empty like this:

IlluminateDatabaseEloquentCollection {#431
     all: [
       AppItem {#4930
         itemCode: "PRD-1",
         itemName: "Watch",
         price_list_details: IlluminateDatabaseEloquentCollection {#4947
           all: [],
         },
       },
     ],
   }

I’m using itemCode as primary key on Item model: protected $primaryKey = 'itemCode';

This is the relationship in Item.php:

    public function price_list_details()
    {
        return $this->hasMany(PriceListDetail::class, 'item_itemCode', 'itemCode');
    }

This is the relationship in PriceListDetail.php:

    public function item()
    {
        return $this->belongsTo(Item::class);
    }

PS. I tried also with:

Item::select('itemCode', 'itemName')->where('itemCode', 'PRD-1')->with(['price_list_details' => function($query){
      $query->select('id');
   }])->get();

Source: Laravel

Leave a Reply