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

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

stanfortonski/laravel-roles
===========================

Users Roles for Laravel Applications. Laravel 7.x and 8.x.

v3.1.0(5y ago)1574↓100%MITPHPPHP &gt;=7.2

Since Nov 11Pushed 4y ago1 watchersCompare

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

READMEChangelog (9)DependenciesVersions (10)Used By (0)

Laravel-Roles
=============

[](#laravel-roles)

Users roles for Laravel applications. Compatible with Laravel 7.x and 8.x.

Information
===========

[](#information)

This package provides you to set of middleware, migrations, directives and methods to user model which can you use for build Laravel Application wht different types of users. You can block or allow to use part of your application to for example admin or guest.

Installation
============

[](#installation)

1. First download package `composer require stanfortonski/laravel-roles`.
2. Setup provider. In config/app.php add following code to bottom of providers:

```
'providers' => [
    //...
    Stanfortonski\Laravelroles\ServiceProvider::class
],
```

3. Setup middleware. In app/Http/Kernel.php add following code to bottom of middlewares:

```
protected $routeMiddleware = [
    //...
    'role' => \Stanfortonski\Laravelroles\Middleware\Role::class,
    'roles' => \Stanfortonski\Laravelroles\Middleware\OneOfRoles::class,
    'allofroles' => \Stanfortonski\Laravelroles\Middleware\AllOfRoles::class
];
```

4. Use one of three traits HasRoles, HasRolesIds or HasRolesIdsAdapter in User class. Snippet: `use HasRoles;`, `use HasRolesIds;` or `use HasRolesIdsAdapter;`.
5. Additonal you can publish config file roles.php. Run command: `php artisan vendor:publish --provider="Stanfortonski\Laravelroles\ServiceProvider"`.

Usage
=====

[](#usage)

More about traits HasRoles, HasRolesIds or HasRolesIdsAdapter.
--------------------------------------------------------------

[](#more-about-traits-hasroles-hasrolesids-or-hasrolesidsadapter)

- HasRoles is for roles based on names. Default way.
- HasRolesIds is for roles based on ids (methods suffix is ById or ByIds). HasRolesIds is actually for combining with HasRoles.
- HasRolesIdsAdapter is for roles based on ids but methods are exactly same samed as in HasRoles trait. It is for independent use without HasRolesIds or HasRoles. Purpose of that is middlewares and directives doesn't work with HasRoleIds methods.
- For HasRolesIds you have to use suffix ByIds for multiple or ById for singular. For parameter pass integer or array of integers. Example: `$user->hasRoleById(1)` instead of `$user->hasRole('nameofrole')`
- For HasRolesIdsAdapter you doesn't have to use suffix but for parameter you have to pass integer or array of integers. Example: `$user->hasRole(1)` instead of `$user->hasRole('nameofrole')`

Middleware
----------

[](#middleware)

If you want to determine which user can use the link. You need to use one of three middleware: roles, roles, allofroles.

Examples: (Attention you have to define admin, moderator and writer roles before! Go To Seeding example.)

1. Only admin can access to main page.

```
    Route::get('/', function () {
        return view('welcome');
    })->middleware('role:admin');
```

2. Only Admin or moderator can access to main page.

```
    Route::get('/', function () {
        return view('welcome');
    })->middleware('roles:admin|moderator');
```

3. Only User that have admin and writer roles (all of these) can access to main page.

```
    Route::get('/', function () {
        return view('welcome');
    })->middleware('allofroles:admin|writer');
```

Checking that user has roles
----------------------------

[](#checking-that-user-has-roles)

```
    $result = $user->hasRole('admin');
```

```
    $result = $user->hasOneOfRoles(['admin', 'mod']);
```

```
    $result = $user->hasAllOfRoles(['admin', 'mod']);
```

Adding roles
------------

[](#adding-roles)

```
    $user->addRole('admin');
```

```
    $user->addRoles(['admin', 'writer']);
```

Removing roles
--------------

[](#removing-roles)

```
    $user->removeRole('admin');
```

```
    $user->removeRoles(['admin', 'mod']);
```

Seeding
-------

[](#seeding)

If you want to define yours own roles run this command `php artisan make:seeder RoleSeeder` and next copy and paste below code to database/seeders/RoleSeeder.php.

```
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class RoleSeeder extends Seeder
{
    private $roles = [
        ['name' => 'admin', 'description' => 'User friendly text'], //role I
        //... next role
    ];

    public function run()
    {
        foreach ($this->roles as $role){
            DB::table('roles')->insert($role);
        }
    }
}
```

Then run `composer dump-autoload` and `php artisan migrate --seed`.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

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 ~22 days

Total

9

Last Release

1825d ago

Major Versions

1.1.0 → v2.0.02021-02-01

v2.1.1 → v3.0.02021-05-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/486ac8f1ac33b8a9018d25e1f2828398089d88adfe9890325cc9a5fad17fe9f0?d=identicon)[stanfortonski](/maintainers/stanfortonski)

---

Top Contributors

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

---

Tags

laravellaravel-7laravel-7-packagelaravel-8laravel-8-packagelaravel-frameworklaravel-packagelaravel-rolesmiddlewarepackagepermissonphpphp-libraryphp7privilegesrolesroles-managementroles-permissionstraits-hasroleslaravellaravel 7laravel 8laravel-packagelaravel-pluginrolespermissionsgroupsUserslaravel-module

### Embed Badge

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

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

###  Alternatives

[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)[arcanedev/laravel-auth

Laravel Authorization package.

294.8k1](/packages/arcanedev-laravel-auth)

PHPackages © 2026

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