I am new to Laravel. I have implemented a search with user role and permission. If I selected a role it should display all the users assigned to that role...same way for permission and if I search for the particular user it should display the user details with role and permission.
User with particular role
User::role('admin')->get()
User with particular permission
User::permission('view')->get();
User with name
User::with('roles.permissions')->with('permissions')->where('name' , 'testadmin')->first();
I wanted to know is this correct? Thanks in Advance!
CodePudding user response:
You can get all the users who has roles/Permission by using wherehas
method
To Get all the User with Admin Role
User::with('roles')
->whereHas('roles',function($query){
return $query->whereIn('name',['Admin']);
});
To get users who have view permission
User::with(['permission'])
->whereHas('permission',function($query){
return $query->whereIn('name',['view']);
});