phplaravellaravel-8laravel-permission

Problem with Laravel spatie/laravel-permissions ( HasRole in Model User )


I have a problem with the spatie/laravel-permissions library.

Previously I had it implemented in my system but after doing composer update it stopped working...

The problem is when I add the HasRole in my User model. Everything crashes and I get the error: "Call to a member function first() on array " Making mention of the PermissionRegistrar package file.

Likewise, if I try to enter another route in my system, the error that appears is " Undefined index: name"

It should be noted that I have my model created, my tables in the database and I carry out the package installation process following the documentation, and as I said before, the roles and permissions system worked for me before.

Something I should mention is that I had previously replaced the "name" field with "description" but I had some configuration problems with the library so I ran another migration adding the Name field, which the library requires. After running the migration, everything seemed to work correctly


Solution

  • You probably have a cache issue specifically related to Spatie. If you face any kind of issues when you are seeding your DB, you can add this line at the top of your seed within the run() method

    app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
    

    On the other hand, if you changed something manually, let's say from DB, you can try with artisan command

    php artisan permission:cache-reset
    

    https://spatie.be/docs/laravel-permission/v5/basic-usage/artisan#content-resetting-the-cache

    Note that you need to clear cache even if you are running a fresh install by using

    php artisan migrate:fresh --seed