How to select columns from joined tables: laravel eloquent

后端 未结 3 1740
我寻月下人不归
我寻月下人不归 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:54

    The shortest and more convenient way I guess would be :

    Vehicle::select('id','name','staff_id')->where('id',1)
    ->with('staff:id,name' )->get();
    

    foreign key should present for selection .

    Since Laravel 5.7 you can use with() like this :

    with('staff:id,name' )
    

    for granular selection.

提交回复
热议问题