Common Issues
This page covers the most frequently encountered issues and their solutions when working with Laravel Permission.
Permission/Role not found errors
Problem You’re getting errors like:
There is no permission named 'edit articles' for guard 'web'.
There is no role named 'writer' for guard 'web'.
Solutions
Verify the permission/role exists in the database
php artisan permission:show
Check the guard name matches
// Make sure the guard matches your config
$permission = Permission :: create ([ 'name' => 'edit articles' , 'guard_name' => 'web' ]);
Clear the cache after creating permissions
php artisan permission:cache-reset
Guard does not match errors
Problem The given role or permission should use guard 'web' instead of 'api'.Solution Ensure all your permissions, roles, and users use the same guard: // When creating permissions/roles
$permission = Permission :: create ([ 'name' => 'edit articles' , 'guard_name' => 'web' ]);
$role = Role :: create ([ 'name' => 'writer' , 'guard_name' => 'web' ]);
// Or specify guard when assigning
$user -> assignRole ( 'writer' , 'web' );
$user -> givePermissionTo ( 'edit articles' , 'web' );
See the Multiple Guards documentation for more details.
Permissions not updating immediately
Unauthorized exceptions (403 errors)
Problem User does not have the right permissions.Debugging Steps
Check if user has the permission
dd ( $user -> getAllPermissions ());
dd ( $user -> roles );
Verify middleware is configured correctly
// In app/Http/Kernel.php (Laravel 10 and below)
protected $middlewareAliases = [
'role' => \Spatie\Permission\Middleware\ RoleMiddleware :: class ,
'permission' => \Spatie\Permission\Middleware\ PermissionMiddleware :: class ,
];
Check if HasRoles trait is applied
use Spatie\Permission\Traits\ HasRoles ;
class User extends Authenticatable
{
use HasRoles ;
}
String '123' treated as permission name instead of ID
Problem When passing permission IDs from forms, they’re treated as strings and lookup fails. Solution Convert string IDs to integers when working with UI forms: // Convert array of permission IDs from strings to integers
$permissionIds = collect ( $validated [ 'permission' ]) -> map ( fn ( $val ) => ( int ) $val );
$role -> syncPermissions ( $permissionIds );
This is especially important in v6+ due to improved UUID/ULID support. String IDs are treated as names, not IDs.
Problem Authorizable class 'App\Models\User' must use Spatie\Permission\Traits\HasRoles trait.Solution Add the trait to your User model: use Illuminate\Foundation\Auth\ User as Authenticatable ;
use Spatie\Permission\Traits\ HasRoles ;
class User extends Authenticatable
{
use HasRoles ;
// ... rest of your model
}
Database migration errors
Problem
Index name too long errors on MySQL
Duplicate column errors
Solutions For MySQL index errors: // In the migration, shorten column lengths
$table -> string ( 'name' , 125 ); // instead of 255
Or use InnoDB with utf8mb4: Schema :: create ( 'permissions' , function ( Blueprint $table ) {
$table -> engine = 'InnoDB' ;
// ... rest of migration
});
For duplicate column errors:
Check if you’ve already run the migrations:php artisan migrate:status
Cache issues with Redis/Memcached
Problem Permissions are being shared between different applications or tenants. Solution Set a unique cache prefix for each application: // config/cache.php
'prefix' => env ( 'CACHE_PREFIX' , 'myapp_cache' ),
For multitenancy: // After switching tenants
app () -> make ( \Spatie\Permission\ PermissionRegistrar :: class ) -> initializeCache ();
Wildcard permissions not working
Problem Wildcard permissions like articles.* are not matching articles.edit. Solution
Enable wildcard permissions in config
// config/permission.php
'enable_wildcard_permission' => true ,
Clear cache after enabling
php artisan permission:cache-reset
Verify wildcard syntax
$user -> givePermissionTo ( 'articles.*' );
$user -> can ( 'articles.edit' ); // true
See Wildcard Permissions for more details.
Teams feature not working
Problem Permissions are bleeding across teams or not respecting team boundaries. Solutions
Ensure you’ve run the teams migration
php artisan permission:upgrade-teams
Set the team ID in your middleware
setPermissionsTeamId ( $team -> id );
Configure team resolver in config
// config/permission.php
'teams' => true ,
See Teams Permissions for detailed setup.
Database & Cache Issues
Database Cache Store Users : If you’re using CACHE_STORE=database, you must install Laravel’s cache tables first:php artisan cache:table
php artisan migrate
Without these tables, you’ll encounter errors like Call to a member function perform() on null.
File Cache Permissions : If using the file cache driver and having trouble clearing cache, check filesystem permissions. The PHP CLI user must have write access to cache files.
Migration Errors
Config Error During Migration
If you get an error about accessing static properties during migration:
Error: Access to undeclared static property Spatie\Permission\PermissionRegistrar::$pivotPermission
Solution : Your migration file needs upgrading. The package now uses instance methods instead of static properties.
Backup your current migration
Make a copy of your existing migration file.
Re-publish the migration
php artisan vendor:publish --tag=permission-migrations --force
Compare and merge customizations
If you had customizations (like UUID support or shortened column lengths), re-apply them to the new migration.
Too many database queries
Problem Seeing N+1 queries when checking permissions. Solution Eager load relationships: // Load roles and permissions with users
$users = User :: with ( 'roles.permissions' ) -> get ();
// For a single user
$user -> load ( 'roles.permissions' );
The package automatically caches permissions, but eager loading prevents multiple queries during a request.
Testing Issues
Permissions not working in tests
Problem Permissions work in your application but fail in tests. Solution
Clear cache in tests
public function setUp () : void
{
parent :: setUp ();
app ()[ \Spatie\Permission\ PermissionRegistrar :: class ] -> forgetCachedPermissions ();
}
Create permissions in your test setup
Permission :: create ([ 'name' => 'edit articles' ]);
Role :: create ([ 'name' => 'writer' ])
-> givePermissionTo ( 'edit articles' );
See Testing for more details.
Still Having Issues?
If you’re still experiencing problems:
Check the GitHub Issues for similar problems
Review the Changelog for recent changes
Create a new issue with:
Your Laravel version
Package version
PHP version
Complete error message
Relevant code snippets