Hi all,
I am running the following code in a blade template:
@can('manage-users')
<p>HELLO WORLD</p>
@endcan
...and the text <p>HELLO WORLD</p> is not seen, when I expect that it will be.
I am trying to use role-based permissions, where a permission that I am testing for (called 'manage-users' in this case) is assigned to a role, and a user is assigned to that role. I then use a @can to test whether the currently logged-in user has that permission.
I am logged in (authorised) in a web session of the application as the User with the id 1 (it is the only user in the users table). This user had all of the relevant permissions setup in tinker, eg.
$user = User::first();
$role = Role::create(['name' => 'admin']);
$permission = Permission::create(['name' => 'manage-users']);
$role->givePermissionTo($permission);
$user->assignRole('admin');
In my permissions table I have a record:
id: 1
name: manage-users
guard_name: sanctum
created_at: 2023-02-14 03:08:51
updated_at: 2023-02-14 03:08:51
and in my roles table I have a record:
id: 1
name: admin
guard_name: sanctum
created_at: 2023-02-14 03:05:07
updated_at: 2023-02-14 03:05:07
and in my role_has_permissions table I have a record:
permission_id: 1
role_id: 1
My model_has_permissions table is empty, but in my model_has_roles table I have:
role_id: 1
model_type: App\Models\User
model_id: 1
I have use Spatie\Permission\Traits\HasRoles; at the top of my User.php model class file, and use HasRoles; at the head of the class definition.
I have added Spatie\Permission\PermissionServiceProvider::class, to the 'providers' => [] section of config/app.php
I've tried clearing the caches (there are soooo many in Laravel, I don't think I know them all) including:
$user->forgetCachedPermissions();// to clear the spatie permissions cache for the user
php artisan config:clear
php artisan optimize
rm ./bootstrap/cache/*.php
sudo php artisan cache:clear # always have to use sudo with this one for some reason
php artisan route:clear
php artisan view:clear
...and that's all the caches I know of I think. If anyone knows any others I can try clearing please let me know.
But yes I'm stumped - I have two projects using the same version of spatie-permissions (5.9.1) and one works and one doesn't, using the same code (as far as I can tell). I have copied the code from the old project, where it works, into the new project, where it doesn't.
I have LOG_LEVEL=debug in /.env but I am not seeing anything in the log, and the page is rendering with status code 200.
Does anybody know what might be going on here?
EDIT: Sorry I forgot to say, I have tried editing the database and changing all the guard_name fields to web instead of sanctum, but it doesn't make any difference.