$users = Role::find($role->id)->user->list('id', 'username');
Don't forget that the list method will not return a query, so it has to be the last method called. For example if you want to order by username, you have to do it like this:
$users = Role::find($role->id)->user->orderBy('username')->list('id', 'username');
No did not work,
$users = Role::find($role->id)->user->list('id','role_id', 'firstname', 'lastname'); Results in Call to undefined method Illuminate\Database\Eloquent\Collection::list()
$users = Role::find($role->id)->user->orderBy('firstname', 'ASC')->list('id','role_id', 'firstname', 'lastname'); Results in Call to undefined method Illuminate\Database\Eloquent\Collection::orderBy()
Even tried it with this: $users = $role->find($role->id)->with(array('user' => function($query){ $query->select('id', 'role_id', 'firstname', 'lastname'); }))->get();
But that ends up with zero results and the query looks like:
select id
, role_id
, firstname
, lastname
from users
where users
.role_id
in ('5629bb40-ce93-11e4-8422-940c6d8249ed', '8a5145e8-d01c-11e4-ba4c-e8de2707a099')
Why must something so simple be so difficult?
This works doing it manually: $users = DB::table('users')->select('id','role_id', 'firstname', 'lastname') ->where(array('users.role_id' => $role->id)) ->orderBy('firstname', 'ASC') ->get();
But whats the point of the relationship if I have to code it manually since the point of the relationship is to get the related model data associated with the relationship definition.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community