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

midascodebreaker's avatar

Eager Load nested Relationship and Return Specific Get( array() ) for each field i want for each nested relationship

For Example i Have a Users Table and i Want to Eager Load Nested Relationship but for each nested relationship i Wanna Specify what column i want to return for each relating model is that even possible? Because if you pipe thru get() it will fetch automatically all the column in the nested relationship...

But if i wanna do something like User::with('posts.comments')->get('user_id','user_email','post_id','post_title','comment_id','comment_title','comment_body')

Why do i wanna do something like this? because i want to reduce the number of fields being return to me when i eager load... yes i can use hidden fields , so it will not be return... but i wanna experiment more with Eager loading and Return Specified Field For Each Nested Relationship

If there is Even a Guide or a Real Solution to what i am thinking please Speak Your Mind thanks

0 likes
5 replies
rodrigo.pedra's avatar
Level 56

I don't know where each column belongs to, but I guess you want something like this:

$users = User::with(['posts' => function ($query) {
    $query->select(['id', 'user_id', 'title']); // fields from posts table
    // user_id is needed so eloquent can match the post with its parent user
}, 'posts.comments' => function ($query) {
    $query->select(['id', 'post_id', 'title', 'body']); // fields from comments table, 
    // post_id is needed so eloquent can match the comment with its parent post
}])->get(['id', 'email']); // fields from users table

Read more about it on the docs: https://laravel.com/docs/5.2/eloquent-relationships#constraining-eager-loads

8 likes
midascodebreaker's avatar

Is this the Correct Way of doing this?

User::with(array('card' => function ($query) {
                $query->addSelect(
                    array(
                        'user_id',
                        'face_value',
                        'used',
                        'transferred',
                        'earnings',
                        'redeemed',
                    ));
            },
            'another_relationship' => function ($query){
            $query->addSelect(
            array(
            'column1',
            'column2',
            'column3',
            'column4',
            
            ));
            
            }
            
            ))->get(['id', 'name', 'email', 'mobile', 'parent', 'verified'])
        ];
midascodebreaker's avatar

thank you , coz im amazed with eloquent and jeffrey way havent shown it yet in the series , but diging in the forum i found that the resource i have and yours is identically the same thank you @rodrigo.pedra

kttfdev's avatar

$users = User::with(['posts' , 'posts.comments:id,post_id,title,body'])->get(['id', 'email']);

Please or to participate in this conversation.