Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sajjadali's avatar

How to add more selects in eloquent query

Hi, I build a query and loaded relationships with it then select some columns from a model but want to add/select more columns from related modes/relationships.

for example $user = User::with('comments') ->select('name', 'age') ->addSelect('comment') // i want to add columns from comments model. ->get();

is there any way to select column at this point ?

0 likes
4 replies
Snapey's avatar

using relationships the comments will be loaded as children

if you want them on the same 'level' and not nested then you must use a join

if you only want to load certain column from the child then you can specify this in the with() function

people don't use joins because if user has many comments then the user data has to be repeated for every comment since a row of the result can only contain one comment

sajjadali's avatar

i want to follow eager loading n dont want to use joins

Please or to participate in this conversation.