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

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

surya/laravel-roles
===================

Roles and permissions for authorization

v1.0-alpha(6y ago)02MITPHPPHP &gt;=7.0

Since Jul 25Pushed 6y agoCompare

[ Source](https://github.com/suryaherdiyanto/laravel-roles)[ Packagist](https://packagist.org/packages/surya/laravel-roles)[ RSS](/packages/surya-laravel-roles/feed)WikiDiscussions master Synced yesterday

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

Laravel Role
============

[](#laravel-role)

Roles and permissions management for your Laravel application.

---

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

[](#installation)

```
composer require surya/laravel-roles

```

Setup
-----

[](#setup)

register the `RolesServiceProvider` in `config/app.php`

```
Surya\Role\RoleServiceProvider::class

```

then publish the migration

```
php artisan vendor publish --provider="Surya\Role\RoleServiceProvider" --tag="migrations"

```

migrate

```
php artisan migrate

```

Usage
-----

[](#usage)

After the setup complete, next you've going to register the `Surya\Role\Supports\Roles` trait on `User` model.

```
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Surya\Role\Supports\Roles;

class User extends Authenticatable
{
    use Notifiable, Roles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

```

this trait would be handle your role and permission relationship and adding some functionality for example `assign new role to user`, `remove some role that user associated with`, `adding permissions` for spesific roles etc.

### Assign Role to User

[](#assign-role-to-user)

To assign user for spesific role to might use `assignRole`

```
$user = App\User::find(1);
$user->assignRole('super admin');

```

with `assignRole` if role doesn't exists with that name, it will create new role and save it to database then assigning to user.

> If you reassign your user with other role, it will detach the old and replace the new one.

If you want assign more than one roles to user use `assignRoles` function.

```
$user = App\User::find(1);
$user->assignRoles([2, 4, 6]);

```

> If you use `assignRoles` you need to retrieve or create role with model `Surya\Role\Models\Role` and then pass array id of roles.

#### Other role method

[](#other-role-method)

Check for spesific role.

```
$user->hasRole('super admin');

```

Getting current user role.

```
$user->role()

```

It will return a `Surya\Role\Models\Role` object.

If you want only retrive role name use `getRole`.

```
$user->getRole()

```

Getting all roles associated with user.

```
$user->getAllRoles()

```

It will return collection object of role.

### Give Permission to Role

[](#give-permission-to-role)

```
$user = App\User::find(1);

$user->assignRole('admin');

$user->role()->givePermission('create post');

```

You need to retrieve the `role` object first in order to give permission to spesific role.

Same as role, if you want give more than 1 permission you need to create or retrive it then pass array of permission id to `givePermissions` function.

```
$user = App\User::find(1);

$user->assignRole('admin');

$user->role()->givePermissions([1, 5, 9]);

```

You can pass collection of id or object of permissions.

Authorize action
----------------

[](#authorize-action)

Use the built in laravel function to authorize the user in controller.

```
$this->authorize('create post')

```

or your might using `Gate` facade.

```
Gate::allows('create post')

```

or with `can` directive

```
@can('create post')

@elsecan

```

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

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

Unknown

Total

1

Last Release

2484d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a6bd16b90e01edd5e96cc8d356e5467bd08fe8ab3d9826a48c5eb62e3deda305?d=identicon)[suryaherdiyanto](/maintainers/suryaherdiyanto)

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[casbin/laravel-authz

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

324339.9k4](/packages/casbin-laravel-authz)[wnikk/laravel-access-rules

Simple system of ACR (access control rules) for Laravel, with roles, groups, unlimited inheritance and possibility of multiplayer use.

103.6k1](/packages/wnikk-laravel-access-rules)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[alajusticia/laravel-logins

Session management in Laravel apps, user notifications on new access, support for multiple separate remember tokens, IP geolocation, User-Agent parser

2011.0k](/packages/alajusticia-laravel-logins)

PHPackages © 2026

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