---
title: "Backed enums — Permissions for Laravel | Roundly"
description: "Pass your own backed enums anywhere a role or permission name is accepted — persisted as the enum’s value."
url: https://roundly-consulting.com/open-source/docs/permissions-for-laravel/enums
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Permissions for Laravel](https://roundly-consulting.com/open-source/docs/permissions-for-laravel.md)

# Backed enums

Every name-taking method accepts a string or a BackedEnum, so you can pass your own permission and role enums (persisted as ->value):

```php
use RoundlyConsulting\Enums\Helpers;

enum PermissionName: string
{
    use Helpers;

    case ViewUsers = 'auth.users.view';
    case EditUsers = 'auth.users.edit';
}

Permissions::for($role)->givePermissionTo(PermissionName::ViewUsers);
Permissions::exists(PermissionName::ViewUsers);
$user->hasPermissionTo(PermissionName::ViewUsers);
```

The Helpers trait from enums-for-laravel is optional — any PHP backed enum works.

## Roles and permissions as enums

Permissions::role() and permission(), every grant and role verb, the checks and Permissions::exists() all take enum cases:

```php
use RoundlyConsulting\Permissions\Facades\Permissions;

enum RoleName: string
{
    case Administrator = 'administrator';
    case Editor = 'editor';
}

Permissions::permission(PermissionName::ViewUsers);
Permissions::for(Permissions::role(RoleName::Editor))->givePermissionTo(PermissionName::ViewUsers);

Permissions::for($user)->assignRole(RoleName::Editor);
$user->hasRole(RoleName::Editor);                   // true
$user->hasPermissionTo(PermissionName::ViewUsers);  // true
Permissions::exists(PermissionName::ViewUsers);     // true
```

## In route middleware

Middleware strings take the enum’s value:

```php
Route::get('/users', UsersController::class)
    ->middleware('can:'.PermissionName::ViewUsers->value);
```

## Seeding from an enum

Keep the enum as the single source of truth and register every case in one call — from a seeder, a deploy step or a service provider. syncFrom() registers permissions, syncRolesFrom() roles, and both return a SyncResult:

```php
use RoundlyConsulting\Permissions\Facades\Permissions;

$result = Permissions::syncFrom(PermissionName::class);
$result->created;   // ['auth.users.edit'] — names registered now
$result->existing;  // ['auth.users.view'] — names that were already there
$result->changed(); // true

Permissions::syncRolesFrom(RoleName::class); // the same, for roles
```

The sync is additive: it never deletes or renames a row the enum does not name, because other services may register their own permissions in the same tables. Rows are created through your configured models, so the cache invalidates itself. Anything that is not a backed enum throws a PermissionException.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/permissions-for-laravel.md)
