PHPackages                             syftnex/laravel-role-enums - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. syftnex/laravel-role-enums

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

syftnex/laravel-role-enums
==========================

Infrastructure to use PHP Backed Enums as roles in Laravel.

v1.1.0(6d ago)21↓100%MITPHPPHP ^8.2CI passing

Since Mar 17Pushed 6d agoCompare

[ Source](https://github.com/syftnex/laravel-role-enums)[ Packagist](https://packagist.org/packages/syftnex/laravel-role-enums)[ Docs](https://github.com/syftnex/laravel-role-enums)[ RSS](/packages/syftnex-laravel-role-enums/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

Laravel Role Enums
==================

[](#laravel-role-enums)

[![Test](https://github.com/syftnex/laravel-role-enums/actions/workflows/ci.yml/badge.svg)](https://github.com/syftnex/laravel-role-enums/actions/workflows/ci.yml)[![Latest Version](https://camo.githubusercontent.com/6fcde005059f7f06c10847020467938ac5dcb852b458bd6294364229f6a3bf2b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737966746e65782f6c61726176656c2d726f6c652d656e756d732e737667)](https://packagist.org/packages/syftnex/laravel-role-enums)[![PHP](https://camo.githubusercontent.com/87a9b94eb012dd8b2e6f9b91a6ceda9768e1bb9ea33ea60f540c3490534683ad/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322b2d626c7565)](https://php.net)[![Laravel](https://camo.githubusercontent.com/88ce7f9ac798288a91de4918224da3cf354ffc092c40118a331bb5fa3c59f968/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d3131253230253743253230313225323025374325323031332d726564)](https://laravel.com)[![License](https://camo.githubusercontent.com/bc96c0ad233735aa5d755eb149b0ee6b606638be10dbdea3832a97c86d51990c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f737966746e65782f6c61726176656c2d726f6c652d656e756d732e737667)](LICENSE)

Infrastructure package to use PHP Backed Enums as roles in Laravel.

This package ships **infrastructure only**. It does not ship any pre-defined roles.

Installation
------------

[](#installation)

```
composer require syftnex/laravel-role-enums
```

Publish package files:

```
php artisan vendor:publish --tag=role-enums
```

Configuration
-------------

[](#configuration)

`config/role-enums.php`

```
return [
    'middleware_alias' => 'role',
    'default_column' => 'role',
    'directives' => [
        'role' => 'role',
        'unless_role' => 'unlessRole',
        'end_unless_role' => 'endUnlessRole',
    ],
    'enums' => [
        'platform' => \App\Enums\PlatformRole::class,
        'org' => \App\Enums\OrgRole::class,
    ],
];
```

Route Middleware
----------------

[](#route-middleware)

```
Route::middleware('role:platform,super_admin')->group(function () {
    Route::get('/admin', AdminController::class);
});

Route::middleware('role:platform,admin,super_admin')->group(function () {
    Route::get('/ops', OpsController::class);
});

Route::middleware('role:org,owner,org_role')->group(function () {
    Route::resource('/projects', ProjectController::class);
});

Route::middleware('role.min:platform,admin')->group(function () {
    Route::get('/reports', ReportsController::class);
});
```

- `role:{alias},{value}[,{value2},...][,{column}]` does strict match checks and accepts OR logic across multiple values.
- `role.min:{alias},{value}[,{column}]` calls `isAtLeast()` when available on your enum.
- For explicit columns, keep the column as the final parameter (example: `role:org,owner,org_role`).

Blade Directives
----------------

[](#blade-directives)

```
@role('org', 'admin')
    Invite Member
@endrole

@unlessRole('platform', 'restricted')
    Dashboard
@endUnlessRole
```

Blade checks are guest-safe and return false when no user is authenticated.

Optional directive renaming in config:

```
'directives' => [
    'role' => 'roleCheck',
    'unless_role' => 'unlessRoleCheck',
    'end_unless_role' => 'endUnlessRoleCheck',
],
```

HasRole Trait
-------------

[](#hasrole-trait)

```
use Syftnex\RoleEnums\Traits\HasRole;

class User extends Authenticatable
{
    use HasRole;

    protected array $roleColumns = [
        'platform' => 'platform_role',
    ];

    protected $casts = [
        'platform_role' => \App\Enums\PlatformRole::class,
    ];
}
```

```
$user->hasRole('platform', 'super_admin');
$user->getRole('platform');
$user->isAtLeastRole('platform', 'admin');

User::whereRole('platform', 'admin')->get();
User::whereNotRole('platform', 'viewer')->get();
User::whereAtLeastRole('platform', 'admin')->get();
```

If an enum does not implement `isAtLeast()`, `isAtLeastRole()` throws `UnsupportedOperationException`. `whereAtLeastRole()` throws the same exception when the enum does not support hierarchy checks.

Enum options() helper
---------------------

[](#enum-options-helper)

```
use Syftnex\RoleEnums\Concerns\ProvidesOptions;
use Syftnex\RoleEnums\Contracts\HasOptions;

enum PlatformRole: string implements HasOptions
{
    use ProvidesOptions;

    case SuperAdmin = 'super_admin';
    case Admin = 'admin';
    case Viewer = 'viewer';
}

PlatformRole::options();
// ['super_admin' => 'Super Admin', 'admin' => 'Admin', 'viewer' => 'Viewer']
```

Generator
---------

[](#generator)

```
php artisan make:role-enum UserStatus --migration=users --trait
```

Generates:

- `app/Enums/UserStatus.php`
- `database/migrations/*_add_user_status_to_users_table.php`
- `app/Traits/HasUserStatusRole.php`

Generated stubs are parseable PHP and include commented guidance for enum cases, optional hierarchy (`isAtLeast`), and model cast examples.

What This Package Does Not Do
-----------------------------

[](#what-this-package-does-not-do)

- Does not ship pre-defined role enums or cases.
- Does not create role/permission tables.
- Does not define how roles map to permissions.
- Does not replace Gates or Policies.

Quality Commands
----------------

[](#quality-commands)

```
composer test
composer lint
composer analyse
composer coverage
```

`composer coverage` requires an installed PHP coverage driver (`xdebug` or `pcov`).

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance99

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

6d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1625c91caafc6d4030f4205a3d73eba1ca6b301d43f630ba94ab7ca734eaffd1?d=identicon)[syftnex](/maintainers/syftnex)

---

Top Contributors

[![aahmadkhokhar](https://avatars.githubusercontent.com/u/256070844?v=4)](https://github.com/aahmadkhokhar "aahmadkhokhar (4 commits)")

---

Tags

backed-enumlaravellaravel-packagelaravel-rolesphp-enumphp-enum-helper-traitlaravelenumauthorizationrolesbacked-enum

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/syftnex-laravel-role-enums/health.svg)

```
[![Health](https://phpackages.com/badges/syftnex-laravel-role-enums/health.svg)](https://phpackages.com/packages/syftnex-laravel-role-enums)
```

###  Alternatives

[shanmuga/laravel-entrust

This package provides a flexible solution to add ACL to Laravel

68304.1k2](/packages/shanmuga-laravel-entrust)[hosseinhezami/laravel-permission-manager

Advanced permission manager for Laravel.

393.1k](/packages/hosseinhezami-laravel-permission-manager)[amdadulhaq/guard-laravel

Guard is Role and Permission management system for Laravel

134.4k](/packages/amdadulhaq-guard-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
