Grant: Enum-Based Roles and Permissions for Laravel
Grant is a roles and permissions package for Laravel by Pushpak Chhajed. Instead of storing permission names as plain strings in the database, you define them as PHP backed enums. The database only stores role assignments per user, and all authorization checks flow through Laravel's Gate — meaning can(), Gate::authorize(), and Blade's @can directive work without any changes to your existing code.
Defining Roles and Permissions as Enums
Grant introduces two contracts: Ability for permissions and Role for roles. A permission enum implements Ability, with each case's backing string value registered as a Gate ability. A role enum implements Role and declares which permissions it grants via a permissions() method:
use Shipfastlabs\Grant\Ability;
use Shipfastlabs\Grant\Role as RoleContract;
enum Permission: string implements Ability
{
case EditPosts = 'edit-posts';
case ViewReports = 'view-reports';
public function deniedMessage(): string
{
return 'You are not allowed to do that.';
}
}
enum Role: string implements RoleContract
{
case Editor = 'editor';
case Viewer = 'viewer';
public function permissions(): array
{
return match ($this) {
self::Editor => [Permission::EditPosts],
self::Viewer => [Permission::ViewReports],
};
}
}
Grant uses roles for assignment and permissions for checks. There is no @role Blade directive or role middleware — you protect routes and actions using Laravel's standard authorization features.
Global and Scoped Role Assignments
After adding the HasRoles trait to your user model, you manage assignments with grant(), revoke(), and syncRoles(). Passing an Eloquent model via the on parameter scopes the role to a specific team, project, or any other model:
$user->grant(Role::Editor);
$user->grant(Role::Viewer, on: $team);
$user->can(Permission::EditPosts);
$user->can(Permission::ViewReports, $team);
When checking a scoped permission, Grant considers both the user's global roles and any roles assigned on that specific model.
Combining Permissions with Policies
Grant does not replace policies — it complements them. The #[Requires] attribute lets you gate a policy method on a permission before the method's own logic runs:
use Shipfastlabs\Grant\Requires;
#[Requires(Permission::EditPosts)]
public function update(User $user, Post $post): bool
{
return $post->author_id === $user->id;
}
Grant forwards Gate arguments to the permission check, including any model used for scoped roles. You can also designate one role as a super admin; only a globally assigned super admin bypasses Gate checks entirely.
Installation
Grant requires PHP 8.3 or later and Laravel 12 or 13:
composer require shipfastlabs/grant
php artisan grant:install
php artisan migrate
The install command publishes the configuration and migration, and generates starter enum files if they do not already exist. After that, add HasRoles to your user model.
Important: If you rename or remove an enum case, existing database rows referencing the old backing value become orphaned. Run php artisan grant:sync to remap or clean up those rows. Use grant:list and grant:show to inspect registered permissions and a user's current roles.
Key Takeaways
- Roles and permissions are PHP backed enums — typo-proof and IDE-friendly.
- All checks go through Laravel's Gate, so no new APIs to learn.
- Scoped roles let you assign different access per team or resource.
- The
#[Requires]attribute combines permission checks with policy logic cleanly. - Renaming enum cases requires a
grant:syncrun to avoid orphaned assignments. - Requires PHP 8.3+ and Laravel 12 or 13.
Source: Grant: Enum-Based Roles and Permissions for Laravel — Laravel News