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

binggle's avatar

how to use 'select' with 'with' ?

Hi . I want to get builder with eloquent builder not using join.

        return User::query()
                ->join('roles', 'users.role_id', '=', 'roles.id')
                ->select('users.*', 'roles.name as role');

it works.

But I want to use simple way using 'with' .

        return User::query()
                ->with('role')
                ->select('users.*', 'roles.name as role');

But it shows error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.name' in 'field list'

roles table has 'name' field.

How can I do this ?

0 likes
10 replies
tykus's avatar

Eager-loading using with results in a separate query. You User object(s) will have a role property; you can get the role name through that:

$user->role->name;
binggle's avatar

@tykus Can you explain more in detail ?

What is the meaning of 'separate query' ?

tykus's avatar

@binggle eager-loading a relation means you have a separate query; see for yourself

\DB::listen(fn ($query) => dump($query->sql, $query->bindings));
$user = User::with('role')->first();

The first query gets the User, the second (separate) query gets the associated Role - Eloquent then loads the role into the User instance so it available as a property

binggle's avatar

@tykus Thanks for so fast reply.

Actually I am using 'power-components/livewire-powergrid' package.

I have to return Eloquent Builder not Collection.

please see the followings.

public function datasource(): ?Builder
{
    return User::query()
            ->join('roles', 'users.role_id', '=', 'roles.id')
            ->select('users.*', 'roles.name as role');

}

But I want to make simple something like..

public function datasource(): ?Builder
    return User::query()
            ->with('role')
            ->select('users.*', 'roles.name as role');
}

User model and Role Model has same field, name.

I would like to make it simple and not using join statement.

tykus's avatar

@binggle I suppose you are working with Eloquent Model objects in the view anyway, so:

public function datasource(): ?Builder
{
    return User::('role'); // use $user->role->name in the view
}

If you are not working then the join is okay too:

public function datasource(): ?Builder
    return User::select('users.*', 'roles.name as role_name');  // $user->role_name
}
binggle's avatar

@tykus yes. this is working a half.

public function datasource(): ?Builder
{
    return User::with('role'); // use $user->role->name in the view
}

But the problem is that two table are using same field , 'name' .

'roles.name' should be returned explicitly together with users all field in query .

That is why I asked how to use 'select with with ' ..

I guess there could be a solution.

Thanks..

tykus's avatar

@binggle if this...

'roles.name' should be returned explicitly together with users all field in query .

then... eager-loading is not the solution, you must alias one of the name columns 🤷‍♂️

binggle's avatar

@tykus

Ah. You really genius. Yes You are right.

I added an accessor on Role Model.

    public function getRoleNameAttribute()
    {
        return $this->attributes['name'];
    }

It returns 'roles.name' with the name of 'roles.role_name' .

Thank you so much.

Please or to participate in this conversation.