How to select columns from joined tables: laravel eloquent

后端 未结 3 1759
我寻月下人不归
我寻月下人不归 2021-01-04 23:08

I have a different problem from this. The scenario is same but I am in need of more filtration of the results.

Let me explain.

Consider I have 2 tables

3条回答
  •  暖寄归人
    2021-01-04 23:55

    Finally found it.. In the ->get() you have to put the 'staff_id' like this

    Vehicle::where('id',1)
                ->with(['staff'=> function($query){
                                // selecting fields from staff table
                                $query->select(['staff.id','staff.name']);
                              }])
                ->get(['id','name','staff_id']);
    

    Since I didn't take the staff_id, it couldn't perform the join and hence staff table fields were not shown.

提交回复
热议问题