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

FREDERIC LD's avatar

Eager loading roles with admin users

Hi,

I am running this query

$institutionsList = Institution::select('id', 'name')->where('someid', '=', $someid)->with('admins:first_name,last_name,admins.uuid')->orderBy('name')->get();

It returns a list of institutions with the related admins

As I am using Spatie/permissions, I would also like to get the roles, How can I do that?

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

You can eager-load nested relations using dot-syntax, so, if the Admin class has a roles relation it would be admins.roles like so:

$institutionsList = Institution::select('id', 'name')
	->where('someid', '=', $someid)
	->with('admins:first_name,last_name,admins.uuid, admins.roles')
	->orderBy('name')
	->get();
FREDERIC LD's avatar

Great. thank you!

the solution was

->with('admins:first_name,last_name,admins.uuid','admins.roles')
FREDERIC LD's avatar

whoops, I meant

->with('admins:first_name,last_name,uuid')->with('admins.roles:name')
1 like

Please or to participate in this conversation.