Geoff
Geoff

Reputation: 6629

Laravel pluck with relations fails

I have tried the following

     $trucks = TttatTblTrucks::with('trucktype')
        ->whereBetween('created_at',[$from, $to])
        ->pluck('trucktype.description')
    ;

and getting an error Unknown column 'trucktype.description'

In my TttatTblTrucks

public function trucktype(){
    return $this->hasOne('App\TblTruckTypes','id','truck_category');
}

In my table i have

tbl_trucks //model is TttatTblTrucks

id, truck_category ....

In my table

truck_category //model is TblTruckTypes
 id, description, ...

Where am i going wrong during pluck as the sam query with get instead of pluck returns

        {
        "id": 2207,
        "truck_category": 28,
        "trucktype": {
            "id": 28,
            "description": "mmnuakrt",
        }
    },

Upvotes: 0

Views: 3279

Answers (1)

Amarnasan
Amarnasan

Reputation: 15529

Ok, I don't exactly know why, but I can't "refine" using Eloquent Query function pluck, but using Collection function lists:

$trucks = TttatTblTrucks::with('trucktype')->whereBetween('created_at',[$from, $to])->get()->lists('trucktype.description');

I guess it must be related with the fact that with is modifying the result and you longer can refine on a model, but on the returned collection.

By the way: lists is not deprecated on Collections. It's only that the function lists has been renamed to pluck on Eloquent Queries, but it remains as lists on Collections (https://laracasts.com/discuss/channels/laravel/lists-deprecated-replacement).

Upvotes: 1

Related Questions