How to select columns from joined tables: laravel eloquent

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

    You can use normal join like this:

    Vehicle::join('staff','vehicles.staff_id','staff.id')
             ->select(
                      'vehicles.id',
                      'vehicles.name',
                      'staff.id as staff_id',
                      'staff.name'
              )
             ->get();
    

    Since, you can't take both id at once in a join because only one is allowed. So, you can staff's id as staff_id.

    You can add vehicle id condition with where clause like:

    Vehicle::join('staff','vehicles.staff_id','staff.id')
             ->where('vehicle.id',1)
             ->select(
                      'vehicles.id',
                      'vehicles.name',
                      'staff.id as staff_id',
                      'staff.name'
              )
             ->get();
    

    Hope you understand.

提交回复
热议问题