Skip to content
Advertisement

How to select columns from joined tables: laravel eloquent

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

vehicles

JavaScript

staffs

JavaScript

I want to select only id and name from both tables(Models). The Vehicle Model contain a belongsTo relation to Staff model.

JavaScript

and I joined using this

JavaScript

When I put fields in ->get() like this

JavaScript

it filters the vehicle table but produce no result of Staff table.

Any ideas?

Advertisement

Answer

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

JavaScript

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement