Showing multiple roles assigned to a user in Laravel using Spatie Permissions

1.6k Views Asked by At

does anyone know how to show multiple roles that are under a user? I have setup my permissions so a user can be under multiple roles, which is set easily enough using the following

$user->assignRole(['Root', 'IT', 'HR']); // assigning roles

I am however having trouble showing all roles a user relongs to in the same way? Has anyone done this and knows how to? I simply want to show on a page which role a user belongs to.

3

There are 3 best solutions below

0
On BEST ANSWER

Refer to this link

https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage

Get the names of the user's roles

$roles = $user->getRoleNames(); 

Returns a collection

1
On

if there are two tables (roles and user_roles) than you can check through inner join ('inner join' to check role must exist in 'roles' table that is assigned to a user) by passing user_id (user_id to get specific roles assigned by a user).

0
On

You can get a collection of roles assigned to the user via $user->roles