Lachlan Young
Lachlan Young

Reputation: 159

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

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.

Upvotes: 1

Views: 1912

Answers (3)

Vicky Gill
Vicky Gill

Reputation: 734

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

Upvotes: 3

Atif Mahmood
Atif Mahmood

Reputation: 390

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).

Upvotes: 2

ibra
ibra

Reputation: 433

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

Upvotes: 1

Related Questions