Laravel Spatie Roles: get the current user's role
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel Spatie Roles: Retrieving the Current User's Role with Ease
Introduction:
In a Laravel-based application that utilizes the Spatie/Roles library, it is essential to know how to handle user roles for security and access management. In this comprehensive guide, we will explore the various ways to get the current user's role, ensuring your code remains robust and secure.
1. Using HasRole Method:
One of the simplest methods to determine a user's role is by using Laravel's built-in `hasRole()` method. This approach requires you to specify the desired role as an argument within the `if` statement. Here's how it looks like in your controller or view:
```php
// Example code
if ($user->hasRole('admin')) { // Do something }
if (Auth::user()->hasRole('admin', 'editor')) { // Check for multiple roles }
```
2. The Auth Facade and Role Providers:
Laravel provides a convenient way to interact with the Roles package via Laravel's built-in authentication system called Auth Facades. To use this approach, you need to configure your application's AppServiceProvider to work with Spatie/Roles. This setup is documented in the official Laravel documentation and can help you gain more insight into working with roles programmatically.
3. Getting All User Roles:
If you want to retrieve all roles assigned to a specific user, Laravel provides a convenient method for doing so. Here's how you can access the list of roles for the current user:
```php
// Example code
$roles = Auth::user()->getRoles(); // Get roles for the authenticated user
foreach ($roles as $role) { // Iterate over all roles }
```
4. Using Laravel's Gate System:
Laravel has a powerful built-in feature called Gates or gates that allows you to restrict access to specific actions based on the current role of the user. To use this method, you need to define your gate rules and associated permissions in the `App\Providers\AuthServiceProvider` file. Then, you can check if the user has a specific permission before executing actions:
```php
// Example code
if (Gate::allows('manage-users')) { // Do something }
```
5. Advanced Role Management with Spatie/Roles:
Spatie/Roles offers additional features and functionality that enables you to manage roles in a more flexible way, such as moving between role assignments or setting default roles for new users. To use these advanced functionalities, consult the official documentation for the package on GitHub.
Conclusion:
Laravel Spatie Roles provides various methods to get the current user's role and manage them effectively. You can utilize the `hasRole()` method or leverage Laravel's built-in authentication system (Auth Facade) with the Role Providers configuration. Additionally, you may want to consider using the Gate System and advanced features of Spatie/Roles to further secure your application. Regardless of which approach you choose, always remember that the primary focus is on maintaining a robust security framework while providing seamless user experiences in your Laravel application.