In the previous tutorial, I discussed laravel eager loading with the specific column. But in this tutorial, I will show you laravel nested eager loading specific columns. So how we can fetch specific column from our nested relationship in laravel. Assume we have a Post model and there is a relationship between comments and the author.

Now we need to fetch or we need to select only a specific column rather than all. Do you know how laravel nested eager loading with selected columns? If you do not know then how to select a specific column in laravel nested eloquent relationship tutorial is for you.

laravel-nested-eager-loading-specific-column

Let's see the example code of laravel nested eager loading specific columns:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;

class TutorialController extends Controller
{   
    public function index()
    {   
        return Post::with([
            'comments:id,user_id,title', 
            'comments.user:id,email'
        ])->first();
    }
}

 

Read also: Eloquent Eager Loading Specific Columns In Laravel

 

Conclusion

Now we know how to select a specific column in laravel nested eloquent relationship. Hope this laravel nested eager loading with selected columns tutorial will help you to create laravel nested eager loading specific columns.