We can select eloquent eager loading specific columns from lazy loading as well as eager loading. In this tutorial, I will use eager loading and also lazy loading to show you eloquent eager loading specific columns. So from this laravel eager loading with selected columns tutorial, you will see select specific columns in laravel eloquent relationship with example code so that you can use this code in your laravel application.

It is good to select specific columns without loading all the unnecessary columns in laravel relational query. So eager loading specific columns laravel is the key part of this tutorial. 

laravel-eloquent-eager-loading-specific-column

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

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;

class TutorialController extends Controller
{   
    public function index()
    {   
        return User::with('posts:id,user_id,title')->first();
    }
}

 

Now see the output:

{
    "id": 1,
    "name": "Candida Hickle I",
    "email": "jocelyn.murazik@example.net",
    "email_verified_at": "2023-01-15 04:59:17",
    "password": "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
    "status": "active",
    "remember_token": "TkkAdiD8AA",
    "created_at": "2023-01-15T04:59:17.000000Z",
    "updated_at": "2023-01-15T04:59:17.000000Z",
    "posts": [
        {
            "id": 1,
            "user_id": 1,
            "title": "Maxime non architecto quia in corporis iusto odio beatae. Harum cupiditate libero dicta et fuga vero. Autem facere id reprehenderit consectetur."
        }
    ]
}

 

Now see the example code of laravel lazy loading with specific columns:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;

class TutorialController extends Controller
{   
    public function index()
    {   
        $user = User::first();

        return $user->load('posts:id,user_id,title');
    }
}

 

You will see the exact same output like before:

{
    "id": 1,
    "name": "Candida Hickle I",
    "email": "jocelyn.murazik@example.net",
    "email_verified_at": "2023-01-15 04:59:17",
    "password": "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
    "status": "active",
    "remember_token": "TkkAdiD8AA",
    "created_at": "2023-01-15T04:59:17.000000Z",
    "updated_at": "2023-01-15T04:59:17.000000Z",
    "posts": [
        {
            "id": 1,
            "user_id": 1,
            "title": "Maxime non architecto quia in corporis iusto odio beatae. Harum cupiditate libero dicta et fuga vero. Autem facere id reprehenderit consectetur."
        }
    ]
}

 

Read also: How To Implement Lazy Loading Relationship In Laravel?

 

Conclusion

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