PHPackages                             codinglabsau/laravel-roles - 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. codinglabsau/laravel-roles

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

codinglabsau/laravel-roles
==========================

Simple Laravel roles

v2.6.1(7mo ago)776.5k↓36.9%11MITPHPPHP ^8.3CI passing

Since Mar 19Pushed 7mo ago3 watchersCompare

[ Source](https://github.com/codinglabsau/laravel-roles)[ Packagist](https://packagist.org/packages/codinglabsau/laravel-roles)[ Docs](https://github.com/codinglabsau/laravel-roles)[ RSS](/packages/codinglabsau-laravel-roles/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (16)Used By (1)

Laravel Roles
=============

[](#laravel-roles)

[![Latest Version on Packagist](https://camo.githubusercontent.com/92f9f5b69249bd9c8806ff398a20a74df0ae329229f018b015fe119b50a2023b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64696e676c61627361752f6c61726176656c2d726f6c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codinglabsau/laravel-roles)[![Test](https://github.com/codinglabsau/laravel-roles/actions/workflows/run-tests.yml/badge.svg)](https://github.com/codinglabsau/laravel-roles/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/4b1fc5416ae77d6dac36fc5bb7d253d21061eadd58a2972bf1522207a6fca36d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64696e676c61627361752f6c61726176656c2d726f6c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codinglabsau/laravel-roles)

A simple, flexible roles implementation for Laravel.

See [v2.3 for Laravel 6-9 support](https://github.com/codinglabsau/laravel-roles/releases/tag/v2.3.0).

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

[](#installation)

### Install with composer

[](#install-with-composer)

```
$ composer require codinglabsau/laravel-roles
```

### Publish migrations and migrate

[](#publish-migrations-and-migrate)

```
php artisan vendor:publish --tag="roles-migrations"
php artisan migrate
```

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

[](#configuration)

If you need to override the default `Role` model, you can do that by publishing the config and setting the `models.role` option.

```
php artisan vendor:publish --tag="roles-config"

```

Usage
-----

[](#usage)

### Add the trait

[](#add-the-trait)

Add the `HasRoles` trait to your user model:

```
use Codinglabs\Roles\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasRoles;
}
```

### Create roles

[](#create-roles)

```
$role = \Codinglabs\Roles\Role::create(['name' => 'manager']);
```

### Get roles

[](#get-roles)

```
$managerRole = \Codinglabs\Roles\Role::whereName('manager')->first();
```

### Associate roles

[](#associate-roles)

Under the hood we are using Eloquent many-to-many relationships.

```
use Codinglabs\Roles\Role;

// attach multiple roles
$user->roles()->attach([
    Role::whereName('employee')->first()->id,
    Role::whereName('manager')->first()->id,
]);

// detach a single role
$user->roles()->detach(Role::whereName('employee')->first());

// update roles to match array
$user->roles()->sync([
    Role::whereName('employee')->first()->id,
]);

// ensure roles in array are attached without detaching others
$user->roles()->syncWithoutDetaching([
    Role::whereName('employee')->first()->id,
]);
```

### Protect routes with middleware

[](#protect-routes-with-middleware)

In `App\Http\Kernel`, register the middeware:

```
protected $routeMiddleware = [
    // ...
    'role' => \Codinglabs\Roles\CheckRole::class,
];
```

And then call the middleware in your routes, seperating multiple roles with a pipe:

```
Route::middleware('role:employee')->...
Route::middleware('role:manager|admin')->...
```

Or with a gate:

```
class UserController extends Controller
{
    public function destroy()
    {
        $this->authorize('role', 'admin');
    }
}
```

Or in the constructor of a controller:

```
class ManagerDashboardController extends Controller
{
    public function __construct()
    {
        $this->middleware('role:manager');
    }
}
```

If the middleware check fails, a 403 response will be returned.

### Check roles on a user

[](#check-roles-on-a-user)

Call hasRole on the user model:

```
// check a single role
$user->hasRole('foo');

// check whether any role exists
$user->hasRole(['bar', 'baz']);

// get all roles
$user->roles;
```

### Conditionally showing content with the blade directive

[](#conditionally-showing-content-with-the-blade-directive)

```
@role('admin')
Super secret admin stuff goes here...
@endrole
```

### Sharing roles with UI (Inertiajs example)

[](#sharing-roles-with-ui-inertiajs-example)

```
// AppServiceProvider.php
Inertia::share([
    'auth' => function () {
        return [
            'user' => Auth::user() ? [
                'id' => Auth::user()->id,
                'roles' => Auth::user()->roles->pluck('name'),
            ] : null
        ];
    }
]);
```

```
// app.js
Vue.mixin({
  methods: {
    hasRole: function(role) {
      return this.$page.auth.user.roles.includes(role)
    }
  }
})
```

```

I am a manager
```

Upgrading from v1 to v2
-----------------------

[](#upgrading-from-v1-to-v2)

Please see [upgrading from v1 to v2](UPGRADING.md) for details and instructions to avoid any issues after upgrading to v2.

Contributing
------------

[](#contributing)

Please see [contributing.md](contributing.md) for details and a todolist.

Security
--------

[](#security)

If you discover any security related issues, create an issue on GitHub.

Credits
-------

[](#credits)

- [Steve Thomas](https://github.com/stevethomas)
- [All Contributors](../../contributors)

License
-------

[](#license)

MIT. Please see the [license file](LICENSE.md) for more information.

About Coding Labs
-----------------

[](#about-coding-labs)

Coding Labs is a web app development agency based on the Gold Coast, Australia. See our open source projects [on our website](https://codinglabs.com.au/open-source).

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance62

Regular maintenance activity

Popularity38

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 89.5% 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 ~155 days

Recently: every ~238 days

Total

14

Last Release

235d ago

Major Versions

v1.2.0 → v2.0.02021-01-29

PHP version history (6 changes)v1.0.0PHP ^7.2.5

v1.2.0PHP ^7.2

v2.0.0PHP ^7.3

v2.1.1PHP ^7.3|^8.0

v2.4.0PHP ^8.1

v2.6.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/6324d6d1a5b089cfa3d7519b590f0762281ff3bad210c6139209a1c6394d6f5c?d=identicon)[stevethomas](/maintainers/stevethomas)

---

Top Contributors

[![stevethomas](https://avatars.githubusercontent.com/u/1127412?v=4)](https://github.com/stevethomas "stevethomas (34 commits)")[![JonathanLouw](https://avatars.githubusercontent.com/u/14814040?v=4)](https://github.com/JonathanLouw "JonathanLouw (3 commits)")[![austincarpenter](https://avatars.githubusercontent.com/u/3736774?v=4)](https://github.com/austincarpenter "austincarpenter (1 commits)")

---

Tags

laravelrolesroles-permissions-managementlaravelroles

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/codinglabsau-laravel-roles/health.svg)

```
[![Health](https://phpackages.com/badges/codinglabsau-laravel-roles/health.svg)](https://phpackages.com/packages/codinglabsau-laravel-roles)
```

###  Alternatives

[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[hasinhayder/tyro

Tyro - The ultimate Authentication, Authorization, and Role &amp; Privilege Management solution for Laravel 12 &amp; 13

6712.1k2](/packages/hasinhayder-tyro)[beatswitch/lock-laravel

A Laravel Driver for Lock.

15529.1k1](/packages/beatswitch-lock-laravel)[codebot/entrust

This package provides a flexible way to add Role-based Permissions to Laravel

1596.6k](/packages/codebot-entrust)

PHPackages © 2026

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