Search code examples
phplaravelpermissionslaravel-permission

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.


Solution

  • 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