Search code examples
laravellaravel-permission

Laravel 8: Problem creating and using a a new Permission


I have a Laravel 8 Project on my Local Windows PC. I uploaded the project to my shared web hosting on Dreamhost via zip file and copying the entire database to remote host. (I am unable to use Composer and php artisan commands on the remote server) I am using Spatie Roles & Permissions in my project.

Later I had to add a new permission 'holiday_vacation' in my project. I created the new permission using artisan commands on my local system I believe that when a new permission is created, it adds a new record in the permissions table and a when a user is given access to a specific permission, a record is added to the model_has_permissions table. I believe that no other table is changed during this process. The newly created 'holiday_vacation' permission works fine on my local system.

However, after I manually updated the remote tables (permissions and model_has_permissions), the remote system is unable to find the new permission (holiday_vacation). The following commands in a controller display error message, "There is no permission named holiday_vacation for guard web."

if(auth()->user()->hasPermissionTo('holiday_vacation') )
{
    dd("Has access");
}

I am absolutely sure that the permissions table has the holiday_vacation permission as I copied the permissions and model_has_permissions tables from the local database to the remote one.

Google search on this issue talks about clearing permission cache (e.g. php artisan cache:forget spatie.permission.cache then php artisan cache:clear). Unfortunately, I can't execute php artisan commands on my shared hosting.

Can someone offer a workaround, please?


Solution

  • @BABAK ASHRAFI's comment did the trick, except that the command needed to be modified slightly. (Ref: https://spatie.be/docs/laravel-permission/v3/advanced-usage/cache)

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