Laravel Permission Extended
Wildcard permissions, super-admin gate, auto middleware registration, Blade directives and Livewire support — all built on spatie/laravel-permission.
Support
| Laravel 10 | Laravel 11 | Laravel 12 | Laravel 13 | |
|---|---|---|---|---|
| PHP 8.2 | ✅ | ✅ | ✅ | - |
| PHP 8.3 | ✅ | ✅ | ✅ | ✅ |
| PHP 8.4 | ✅ | ✅ | ✅ | ✅ |
| PHP 8.5 | — | ✅ | ✅ | ✅ |
1// Wildcards just work in every Spatie method2$user->hasPermissionTo('admin.*');3$user->hasRoleOrPermission('editor|admin.*');4 5// Super-admin bypasses everything via Gate6@can('anything') YES @endcan7 8// Spatie middleware registered automatically9Route::middleware('permission:admin.*')
Installation
1composer require nyoncode/laravel-permission-extended2php artisan permission-extended:install
The install command will publish config, Spatie migrations, run migrations and optionally add the trait to your User model.
Manual Setup
Change the HasRoles import on your User model — from Spatie to this package:
1// Before: use Spatie\Permission\Traits\HasRoles;2// After:3use NyonCode\PermissionExtended\Traits\HasRoles;4 5class User extends Authenticatable6{7 use HasRoles;8}
Do not use both Spatie's and this package's
HasRoleson the same model — this one already includes Spatie's internally.
Features
Wildcard Permissions
1$user->hasPermissionTo('admin.*'); // any admin permission2$user->hasAnyPermission(['admin.*', 'posts.edit']); // any match3$user->hasAllPermissions(['admin.*', 'posts.*']); // all must match4$user->hasRoleOrPermission('editor|admin.*'); // role OR permission
@can('admin.*'), Gate::allows('admin.*') and middleware('permission:admin.*') all work transparently.
Super-Admin Role
Configured via config/permission-extended.php:
1'super_admin_role' => 'super-admin', // or null to disable
Users with this role pass every Gate::allows() / @can() / $user->can() check automatically. No need to assign individual permissions.
Automatic Middleware Registration
Spatie's middleware is registered automatically — no manual setup in bootstrap/app.php:
1// These just work out of the box:2Route::middleware('role:admin')3Route::middleware('permission:admin.*')4Route::middleware('role_or_permission:editor|admin.*')
Disable via 'register_middleware' => false in config.
Blade Directives
1@canPermission('admin.*') @endcanPermission2@canAnyPermission(['a.*', 'b.*']) @endcanAnyPermission3@canAllPermissions(['a.*', 'b.*']) @endcanAllPermissions4@hasRoleOrPermission('admin|a.*') @endhasRoleOrPermission5@unlessCanPermission('admin.*') @endunlessCanPermission
Native @can('admin.*') also works.
Livewire Integration
1use NyonCode\PermissionExtended\Livewire\WithPermissions; 2 3class AdminPanel extends Component 4{ 5 use WithPermissions; 6 7 public function mount() 8 { 9 $this->permAuthorize('admin.*');10 }11}
1@if($this->permCan('posts.*'))2 <button wire:click="create">New</button>3@endif
Cache Management
1php artisan permission:flush
Clears both Spatie's permission cache and the wildcard pattern cache.
Documentation
- Installation
- Wildcard Permissions
- Blade Directives
- Middleware
- Livewire
- Reactive Updates
- Configuration
Testing
1composer test
License
MIT