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

Laraveldeep's avatar

List of Unique Users with Certain Role From Many To Many Relationship

Hi,

I have something like this

users table
---------------
id, <other fields>

roles table
------------------
id, rolename, permission

role_users table
-------------------
roleid, userid

Since I have role and permission on same table, rolename is repeated like this

rolename, permission
admin, siteadmin
admin, useradmin
admin, emailadmin

I have already defined the many to many relationship on both User and Role models.

I need to get a list of unique admins.

What is the most efficient way (less code) to get the result in this case?

Thanks!

0 likes
9 replies
usman's avatar

@Laraveldeep from Users side you can use:

$users = App\User::whereHas('roles', function($query) use ($roleName) {
    $query->where('rolename', '=', $roleName);
})->get();

From Roles side you can use the following code to retreive the users for a role:

$role = App\Role::where('rolename','=', $roleName)->first();
$users = $role->users()->get();

Usman.

1 like
Laraveldeep's avatar

Thanks both for the quick response.

@usman : I got your point, but as I mentioned my rolename is repeated (because of permission). So in this case I will retrieve $roles instead of single role, which I have to break down with foreach loop.

Also for each role, I need to merge the users result somewhere, so was checking the doc on collections

https://laravel.com/docs/5.3/eloquent-collections

https://laravel.com/docs/5.3/collections

I am thinking of something like this:

        $adminroles = Role::where('rolename', 'admin')->get();


        $admins = collect();

        foreach($adminroles as $adminrole)
        {
            $admins->push($adminrole->users()->select('name','email')->get());
        }

        foreach($admins as $admin)
        {
            // list each user
            // but if user have multiple roles it might repeat too
        }

But still I will get duplicate users depending upon multiple role a user will have.

I am not sure if I am doing this correctly.

This looks lengthy...

Is there an alternative for this by using something like groupby and having in eloquent?

or

single line of code which can do all these that I am not aware of?

My goal is to get unique users with certain rolename disregarding the permission

Thanks!

usman's avatar

@Laraveldeep If your role names are being repeated then what is the point of having a many to many relationship. Please first checkout some resource on many to many relationships in an RDBMS and why they exist.

You will also need a many to many relationship for role and permissions.

Regards!

1 like
Laraveldeep's avatar

Oh...

that was a quick decision made thinking it will be easier to check role and permission.

Now I realize that I need to break permission on separate table.

Thanks @usman

elinardo's avatar

@USMAN - hi man.. i try this exemplo and fine, but i'm try a list of patient from user auth, this is Policy? With your exemple ai have all users, but how list just users from auth->user(). this auth->user() hava a relation with a company. It's in resolve in policy? like ai list how you learn in my policy i try something link this: $user->id === $company->user_id?

Please or to participate in this conversation.