spatie/laravel-permission
Manage roles and permissions in Laravel using database-backed models integrated with Laravel’s Gate. Assign roles to users, grant permissions directly or via roles, and authorize actions with the familiar can() checks. Includes docs for setup and usage.
can(), Gate facade), reducing friction in adoption."edit articles") and role-based access control (RBAC), enabling scalable authorization logic for complex applications.web, api) ensures flexibility in microservices or multi-tenant architectures."edit article X") or coarse RBAC (roles only)?web vs. api)?getAllPermissions() queries scale? Consider caching strategies (e.g., Redis).Gate, Policy, and can() methods.composer require spatie/laravel-permission.php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider".php artisan migrate.HasRoles trait to the User model (and other models if needed).config/auth.php (if using multi-guard).// Example: Seed roles/permissions
$admin = Role::create(['name' => 'admin']);
$editPermission = Permission::create(['name' => 'edit content']);
$admin->givePermissionTo($editPermission);
$user->givePermissionTo('edit content');
$this->assertTrue($user->can('edit content'));
User::role('admin')->get()).auth:permission).@can).$user->roles).User model and related models.can() checks).getAllPermissions() if needed.permission:show, permission:cache-reset) aid diagnostics.AuthorizationException) to track misuse or edge cases.getAllPermissions() can be expensive for users with many roles. Mitigate with:
name and guard_name columns are indexed.User::role('admin') are efficient if indexed properly.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database migration failure | Broken permission system | Test migrations in staging; use rollback plans. |
| Cache corruption | Stale permission data | Manual cache reset (php artisan permission:cache-reset). |
| Permission logic bugs | Unintended access/denials | Unit tests for critical permission checks. |
| Multi-guard misconfiguration | Permissions not applying to correct guard | Explicitly set guard_name in seeds/migrations. |
| Enum migration issues | Type safety lost | Gradual adoption; document fallback strings. |
edit article too broad?").How can I help you explore Laravel packages today?