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

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

yasser/laravel-roles
====================

Powerful package for handling roles and permissions in Laravel 5.3

0.1.2(9y ago)237MITPHPPHP &gt;=5.6.4

Since Dec 28Pushed 9y ago1 watchersCompare

[ Source](https://github.com/yasser17/roles)[ Packagist](https://packagist.org/packages/yasser/laravel-roles)[ RSS](/packages/yasser-laravel-roles/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

Roles and Permissions
=====================

[](#roles-and-permissions)

[![Build Status](https://camo.githubusercontent.com/2d0a87cc10a8d20ed04c7cdcc4e5692be6d331f2defd6a35d1e056aa4de0e96c/68747470733a2f2f7472617669732d63692e6f72672f79617373657231372f6c61726176656c2d726f6c65732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yasser17/laravel-roles)

- [Installation](#installation)
    - [Composer](#composer)
    - [Service provider](#service-provider)
    - [Migrations](#migrations)
    - [User trait](#user-trait)
    - [Middleware](#middleware)
- [Usage](#usage)
    - [Create Permissions](#create-permissions)
    - [Create Roles](#create-roles)
    - [Attach and Detach Permissions to a role](#attach-and-detach-permissions-to-a-role)
    - [Attach and Detach Role to a user](#attach-and-detach-role-to-a-user)
    - [Check if a user has a role](#check-if-a-user-has-a-role)
    - [Check if a user has a permission](#check-if-a-user-has-a-permission)
    - [Blade directives](#blade-directives)
    - [Middleware functions](#middleware-functions)

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

[](#installation)

For you can install this package. You should to follow the next steps.

### Composer

[](#composer)

For a installation package you can use the composer command

```
composer require yasser/laravel-roles

```

or you can pull this package in through Composer file

```
{
	"require": {
		...
		"yasser/laravel-roles": "^0.1.2"
	}
}
```

### Service Provider

[](#service-provider)

Add the package to your application service providers in `config/app.php` file.

```
'providers' => [
		...
		/*
         * Package Service Providers...
         */

        Yasser\Roles\RolesServiceProvider::class,

		/*
         * Application Service Providers...
         */
        ...
],
```

### Migrations

[](#migrations)

Excecute this command in your console to add migrations files to a project

```
php artisan vendor:publish --provider="Yasser\Roles\RolesServiceProvider" --tag=migrations

```

and also run the migrations

```
php aritsan migrate

```

### User trait

[](#user-trait)

Include `HasRolesRelations` trait inside your `User` model.

```
use Yasser\Roles\Traits\HasRolesRelations;

class User extends Authenticatable
{
    use Notifiable, HasRolesRelations;

}
```

### Middleware

[](#middleware)

Add the middleware `VerifyPermission` into app/Http/kernel.php file.

```
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
    	...
		'verify' => \Yasser\Roles\Middlewares\VerifyPermission::class,
        'check' => \Yasser\Roles\Middlewares\VerifyRole::class,
    ],
```

Usage
-----

[](#usage)

### Create Permissions

[](#create-permissions)

```
	use Yasser\Roles\Models\Permission;

	$permission = Permission::create([
        'name' => 'Create Users',
        'slug' => 'user.create',
        'description' => '', //optional
        'model' => '' //optional
    ]);
```

### Create Roles

[](#create-roles)

```
	use Yasser\Roles\Models\Role;

	$adminRole = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
        'description' => ''//optional
    ]);
```

### Attach and Detach Permissions to a role

[](#attach-and-detach-permissions-to-a-role)

You can attach one permition to a role

```
	$createPermission = Permission::create([
        'name' => 'Create Users',
        'slug' => 'user.create',
        'description' => '', //optional
        'model' => '' //optional
    ]);

    $role = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    $role->attachPermission($createPermission);
```

or you can attach many permitions to a role

```
	$createPermission = Permission::create([
        'name' => 'Create Users',
        'slug' => 'user.create'
    ]);

	$deletePermission = Permission::create([
        'name' => 'Delete user',
        'slug' => 'user.delete'
    ]);

    $role = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    $role->attachPermissions([$createPermission, $deletePermission]);
```

Detach a one permission from a role

```
	$role->detachPermission($createRole);
```

or you can detach many permissions from a role

```
	$role->detachPermissions([$createPermission, $deletePermission])
```

### Attach and Detach Role to a user

[](#attach-and-detach-role-to-a-user)

Attach a Role to a user

```
	$role = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    $user->attachRole($role)
```

Attach many roles to a user

```
	$adminRole = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    $operatorRole = Role::create([
        'name' => 'Operator',
        'slug' => 'operator',
    ]);

    $user->attachRoles([$adminRole, $operatorRole]);
```

Detach a role from a user

```
	$user->detachRole($role)
```

Detach many roles from a user

```
	$user->detachRoles([$adminRole, $operatorRole])
```

### Check if a user has a role

[](#check-if-a-user-has-a-role)

```
    $adminRole = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    $user->checkRole('admin'); //return true or false
```

### Check if a user has a permission

[](#check-if-a-user-has-a-permission)

```
    $createPermission = Permission::create([
        'name' => 'Create Users',
        'slug' => 'user.create'
    ]);

    $user->canDo('user.create');
```

### Blade directives

[](#blade-directives)

```
    $role = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    @checkRole('admin')
    ...
    @endCheckRole

    $createPermission = Permission::create([
        'name' => 'Create Users',
        'slug' => 'user.create'
    ]);

    @canDo('user.create')
        Create a user
    @endCanDo
```

### Middleware functions

[](#middleware-functions)

```
    $role = Role::create([
        'name' => 'Admin',
        'slug' => 'admin',
    ]);

    Route::get('user/create', ...)->middleware('ckeck:admin');
```

```
    $createPermission = Permission::create([
        'name' => 'Create Users',
        'slug' => 'user.create'
    ]);

    Route::get('/user/create', ... )->middleware('verify:user.create');
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

3

Last Release

3422d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f5b103464ef6997db753b7d59beb95a38d28d9b4e1dee7b08b2a61cd61e94403?d=identicon)[yasser.mussa](/maintainers/yasser.mussa)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[laragear/two-factor

On-premises 2FA Authentication for out-of-the-box.

339785.3k8](/packages/laragear-two-factor)[casbin/laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.

324339.9k4](/packages/casbin-laravel-authz)[yajra/laravel-acl

Laravel ACL is a simple role, permission ACL for Laravel Framework.

112103.9k1](/packages/yajra-laravel-acl)[scaler-tech/laravel-saml2

SAML2 Service Provider integration for Laravel applications, based on OneLogin toolkit

2737.5k](/packages/scaler-tech-laravel-saml2)[directorytree/authorization

Native Laravel Authorization.

1809.5k](/packages/directorytree-authorization)

PHPackages © 2026

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