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

ralphmorris's avatar

Get users that have specific Location. Linked via professional user role.

Hi there,

I wonder if anybody can help me with this?

Say I have 3 tables. users, professionals (a specific user role) and locations.

A professional belongs to a user and a location belongsToMany professionals.

I want to get all the users that have a certain Location associated with it.

I have done the following which does work but I think there must be a more efficient way of doing it?

$location = Location::findOrFail($request->location_id); $professionals = $location->professional()->get();

foreach ($professionals as $pro) { $pro_user = User::findOrFail($pro->user_id); }

I know that I could relate the Locations to the User as opposed to Professional but it just makes sense to me to link it to the Professional model as other users don't really need related Locations. Or does this not matter?

If someone could point me in the right direction I'd really appreciate it!

Thanks in advance,

Ralph

0 likes
0 replies

Please or to participate in this conversation.