Skip to content

Grant: Enum-Based Roles and Permissions for Laravel

Grant is a Laravel package that lets you define roles and permissions as PHP enums, stores assignments in the database, and routes all checks through Laravel's Gate so can(), Gate::authorize(), and @can work out of the box.

Published · Updated · 3 min read

Grant: Enum-Based Roles and Permissions for Laravel

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:sync run to avoid orphaned assignments.
  • Requires PHP 8.3+ and Laravel 12 or 13.

Source: Grant: Enum-Based Roles and Permissions for Laravel — Laravel News

Frequently asked questions

Does Grant replace Laravel's built-in Gate and policies?

No. Grant integrates with Laravel's Gate rather than replacing it. All permission checks use can(), Gate::authorize(), and @can as normal. Policies still handle model-level rules, and Grant's #[Requires] attribute can gate a policy method on a permission before the method's own logic executes.

What happens if I rename or delete a permission or role enum case?

Renaming or removing an enum case leaves any existing database rows that reference the old backing value orphaned, causing affected users to lose that role. You should run php artisan grant:sync after making such changes to remap or delete the stale rows.

Can I scope a role to a specific team or project rather than applying it globally?

Yes. When calling grant(), pass any Eloquent model via the on parameter to scope the role to that model. Permission checks that include the same model will consider both global roles and roles scoped to that model.

Mohamed Said

About the author

Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

About GitHub LinkedIn WhatsApp Email Address My CV

Have a technical challenge?

Tell me what you’re building. I reply within two working days.

Discuss your project