Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jmacdiarmid's avatar

Checking if user has role using Spatie-Permissions

In App/Http/Middleware/HandleInertiaRequests.php, I am trying to assign true or false to a "is_admin" array element. I have a Laravel 10 install with Jetstream, Inertia, and filament. I'm trying to add 'is_admin' => $request->user()->hasRole('Administrator') to the share function as shown below. When I add it, the hasRole function is not found. I added the "hasRole" trait to the User model. Has anyone see this? What I am I doing wrong? Since I have the Laravel Filament package installed and added the "implements FilamentUser" to the User model, I thought that might have something to do with it but it doesn't change anything when I remove it.

    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     * @param Request $request
     * @return array
     */
    public function share(Request $request): array
    {
        return array_merge(parent::share($request), [
            'user.roles' => $request->user() ? $request->user()->roles->pluck('name') : [],
            'user.permissions' => $request->user() ? $request->user()->getPermissionsViaRoles()->pluck('name') : [],
            'is_admin' => $request->user()->hasRole('Administrator'),
        ]);
   }

Update: Here is a bit more clarification. The hasRole() function works if someone with the admin role is logged in bit not when a guest is viewing the page (no one is logged in) so the 'is_admin' propety doesn't exist. When no one is logged in, I get "Call to member function hasRole() on null". I added the following code to one of the links in the nav bar.

 <NavLink v-if="$page.props.is_admin"  :href="route('dashboard')" :active="route().current('dashboard')">
    Admin
 </NavLink>
0 likes
5 replies
LaryAI's avatar
Level 58

The hasRole method is provided by the Spatie-Permissions package. Therefore, you need to make sure that you have installed and configured the package correctly.

To install the package, run the following command:

composer require spatie/laravel-permission

After installing the package, you need to publish the configuration file by running the following command:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"

Then, you need to run the migration to create the necessary tables:

php artisan migrate

Finally, you need to make sure that your User model uses the HasRoles trait:

use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
    
    // ...
}

If you have done all of the above and you are still getting the error, try running the following command to clear the cache:

php artisan cache:clear

If the problem persists, make sure that you have imported the HasRoles trait at the top of your User model:

use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
    
    // ...
}

If you have done all of the above and you are still getting the error, try running the following command to dump the autoload:

composer dump-autoload

If none of the above solutions work, try reinstalling the package by running the following commands:

composer remove spatie/laravel-permission
composer require spatie/laravel-permission

Make sure to configure the package and run the necessary migrations again.

Snapey's avatar
Snapey
Best Answer
Level 122

$request->user() will be null for guests - so not a user object, therefore you cannot call hasRole on it

use the auth check function first

'is_admin' => auth()->check() && auth()->user()->hasRole('Administrator'),
newbie360's avatar

open Tinker type

(array) null
// return []

(bool) null
// return false

so you can use ?->

        return array_merge(parent::share($request), [
            'user.roles' => (array) $request->user()?->roles->pluck('name'),
            'user.permissions' => (array) $request->user()?->getPermissionsViaRoles()->pluck('name'),
            'is_admin' => (bool) $request->user()?->hasRole('Administrator'),
        ]);
jmacdiarmid's avatar

@newbie360 Thank you for your suggestion. I tried this out. It does work however @snapey 's suggestion also works and appears self-documenting.

Please or to participate in this conversation.