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

GodziLaravel's avatar

How to return only some attributes instead of all attributes model

Hello

In task model i would like to return only user name instead of all attributes :

    public function users()
    {
        return $this->belongsToMany(User::class);
    }

Controller :

    public function show($id)
    {
        $task = Task::with([
             'tools', 'users', 'teamleaderCompanyAddress', 'taskType'
        ])->findOrFail($id);
        return $task;
    }

Thanks

0 likes
8 replies
mstrauss's avatar

Hi @mostafalaravel

Sure, try this:

$task = Task::with([
             'tools', 'users:id,user_name', 'teamleaderCompanyAddress', 'taskType'
        ])->findOrFail($id);
        return $task;

See below from the docs: https://laravel.com/docs/master/eloquent-relationships#eager-loading

Eager Loading Specific Columns You may not always need every column from the relationships you are retrieving. For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve:

$books = App\Book::with('author:id,name')->get();

TIP You have to include the id column to make this work, even if you don't need it. So be sure to include it with whatever other columns you need.

1 like
Nakov's avatar

You can just pluck the user name like this:

public function show($id)
{
    $task = Task::with([
         'tools', 'users', 'teamleaderCompanyAddress', 'taskType'
    ])->findOrFail($id);
    
    return $task->users->pluck('name');
}

But then I am just curious, why do you need to load the other relationships??

mstrauss's avatar

@nakov If you look at the OP's original code, they are using the ->findOrFail($id) to get the specified Task based on the id argument passed in via the request. So the eager loading would be valid in this case, as the OP may be using those items.

The original question was:

In task model i would like to return only user name instead of all attributes

So I assumed they were just looking for a way to limit the number of fields returned on the User model in the query.

1 like
GodziLaravel's avatar

@mstrauss

thanks for your answer :

I would like to add the id :

$task = Task::with([
             'tools', 'users:id,first_name,last_name', 'teamleaderCompanyAddress', 'taskType','department'
        ])->findOrFail($id);
        return $task;

But I got this error :

SQLSTATE[42702]: Ambiguous column: 7 ERROR:  column reference "id" is ambiguous
LINE 1: select "id", "first_name", "last_name", "task_user"."task_id...
               ^ (SQL: select "id", "first_name", "last_name", "tas
Nakov's avatar

@mstrauss I misunderstood the question then..

@mostafalaravel The error is because most of the tables have an id, so you can provide an alias for some, you can do it like this then:

$task = Task::with([
             'tools', 'teamleaderCompanyAddress', 'taskType', 'department', 'users' => function($query) {
                   $query->select('users.id as user_id', 'users.first_name', 'users.last_name');
            }
        ])->findOrFail($id);

return $task;

Note the alias of the users.id as user_id so make sure that you don't have the same column name in the Task model.

Snapey's avatar

@nakov what in the query would cause this error, which is normally confined to joins. I see no join here?

Nakov's avatar

@snapey I guess when you use users:id,first_name,last_name adds them as first class properties in this case to the task instance itself. I don't know.

Can you please tell me what causes the error then?

Snapey's avatar

I dont know. It goes against my understanding. I would like to see the full query.

Please or to participate in this conversation.