How To Use Roles & Permissions Tools
Cubeta Starter's roles & permissions feature is built on top of spatie/laravel-permission, a well-established, battle-tested authorization package. The command php artisan cubeta:install permissions does the following for you:
- Requires
spatie/laravel-permissioninto your project via Composer. - Publishes Spatie's own migrations and
config/permission.php(viaphp artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"). - Adds the
Spatie\Permission\Traits\HasRolestrait to yourUsermodel. - Registers the
role,permission, androle_or_permissionmiddleware aliases inbootstrap/app.php(Spatie\Permission\Middleware\RoleMiddleware,PermissionMiddleware, andRoleOrPermissionMiddleware).
After installing, run your migrations:
php artisan migrateRoles & Permissions API
Once installed, your User model (or any model you add HasRoles to) has the full Spatie API available: assignRole(), hasRole(), removeRole(), givePermissionTo(), hasPermissionTo(), can(), and more, plus the Spatie\Permission\Models\Role and Spatie\Permission\Models\Permission models.
auth()->user()->assignRole('admin');
auth()->user()->hasRole('admin'); // bool
auth()->user()->givePermissionTo('edit articles');
auth()->user()->can('edit articles'); // bool2
3
4
5
6
7
This page won't re-document Spatie's full API — refer to the official spatie/laravel-permission documentation for everything else (teams, super-admins, permission caching, Blade directives, etc.).
create:actor
Cubeta Starter still provides php artisan create:actor to register an actor (role) for your project on top of Spatie's models. For a given actor it generates:
- A native PHP enum case on
App\Enums\RoleEnum(e.g.RoleEnum::Admin = 'admin'), plus apermissions(): arraymatch arm describing its declared permissions. - A
RoleSeederthat creates the correspondingSpatie\Permission\Models\Rolerecords:
use App\Enums\RoleEnum;
use Spatie\Permission\Models\Role;
foreach (RoleEnum::cases() as $role) {
Role::firstOrCreate(['name' => $role->value]);
}2
3
4
5
6
- A route group for the actor guarded by Spatie's
rolemiddleware, e.g.'role:admin'(previously'has-role:admin').
Run the seeder after generating an actor:
php artisan db:seed RoleSeederNOTE
create:actor requires the permissions feature to already be installed (cubeta:install permissions), and — for an authentication controller — the auth feature (cubeta:install auth).
Further reading
For anything beyond the basics shown above — permission caching, teams/multi-tenancy, wildcard permissions, Blade directives (@role, @can), and the full middleware syntax — see the spatie/laravel-permission documentation.
