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

Fajar's avatar
Level 2

filltering roles

I am experiencing a problem where I have to introduce each user, the problem is that one user has more than one role, how do I get one of the roles of a user who has more than one role ... for example, Windi has roles as admin and cashier, well here I want to take the data that is the role of the cashier .. please help

0 likes
4 replies
tykus's avatar

How do you decide which role you want; is there a hierarchy?

Fajar's avatar
Level 2

@tykus according to the example above, example windi has the role of admin and cashier

then I want the data that contains the cashier only

if i try this code

$cashirs = User::whereHas('roles', function($role){
                $role->where('roles.name','=','cashirs');
        })->get();

and call

@foreach($cashirs as $get)
                                    <option value="{{$get->id}}">{{$get->roles->implode('name',',')}}</option>
                                @endforeach

and the result

admin,cashirs

Please or to participate in this conversation.