PHPackages                             waiyanhein/lara-role-manager - 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. waiyanhein/lara-role-manager

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

waiyanhein/lara-role-manager
============================

Library to manage the user roles for Laravel

1.0.1(5y ago)1281MITPHP

Since Jun 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/waiyanhein/lara-role-manager)[ Packagist](https://packagist.org/packages/waiyanhein/lara-role-manager)[ RSS](/packages/waiyanhein-lara-role-manager/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

### Package to manage user roles for Laravel application

[](#package-to-manage-user-roles-for-laravel-application)

Having to write code for managing user roles each time you create a new Laravel project is a repeating project. Sometimes, you will try to copy the existing code form the other projects, such as Role model class. You will need to create a model class for role, plus, migration file for it. Then you also have to define the relationship between user and role. Then you will have to write methods for related logic for example, checking if the user belongs to a role. This package includes all the necessary common logic and components fo handling user roles for Laravel application.

#### installation

[](#installation)

- `composer require waiyanhein/lara-role-manager`

Then you need to register `Waiyanhein\LaraRoleManager\LaraRoleManagerServiceProvider` provider in the `config/app.php` file.

#### Publishing config file to define roles

[](#publishing-config-file-to-define-roles)

- `php artisan vendor:publish`

Then roles.php file will be published under the app/config folder. You can define the roles in the following format.

`[ 1 => 'Superadmin', 2 => 'Manager', 3 => 'Staff' ]`The array key is the unique code and the value is the title to be displayed to the end users.

#### Migrating the table

[](#migrating-the-table)

- Then you migrate the tables for the roles. `php artisan migrate`

#### Usages

[](#usages)

You will need to place the `Waiyanhein\LaraRoleManager\Traits\RoleManager` trait into the user model class.

#### Seeding data (Optional)

[](#seeding-data-optional)

If you are seeding the roles into the database, you can seed the data from the `config/roles.php` calling the following method in your seeder class.

- `\Waiyanhein\LaraRoleManager\LaraRolesSeeder::seed();`

#### Methods

[](#methods)

Then you can use the following methods based on your need.

`Waiyanhein\LaraRoleManager\Models\Role` class

- `findRole($code)` - static method. This method will return the role based on the code. $code is the unique code defined in the `config/roles.php` file mapped to the displayed title.
- `users()` - This method is the Eloquent relationship. You can leverage all the features of Eloquent on this method. For example, `$role->users()->get()`.
- `roles()` - static method. This method will return all the roles in the `id` =&gt; `displayed title` mapping. Return type is array.

`Waiyanhein\LaraRoleManager\Traits\RoleManager` trait

You embed this trait into your user model class so that your user model class can leverage all the features of this trait.

- `roles()` - This method is the Eloquent relationship. You can leverage all the features of Eloquent on this method. For example: `$user->roles()->get()`.
- `attachRole($code)` - This will attach a role to the user. $code is the unique code defined in the `config/roles.php` file mapped to the displayed title. For example: `$user->attachRole(User::ROLE_ADMIN)`;
- `attachRoles($codes)` - This is similar to `attachRole` function. Instead, you pass an array of codes to assign multiple roles to the user at the same time.
- `hasRole($code)` - Checks if the user has a role. This method will return boolean value. For example: `$user->hasRole(User::ROLE_ADMIN)`.
- `hasRoles($codes)`. This method is very similar to `hasRole` method. Instead, this will check if the user belongs to all the roles which are pass as an array to the method. If any of the role is missing, it will return false.

#### Note

[](#note)

This package already has a factory class for Role model if you are going to write tests. You do not need to create new factory class for Role model.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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

2160d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ca653867bab097f161b916b2830f0a4e6161541551c16a5ea263549cbcb78cf?d=identicon)[Wai Yan Hein](/maintainers/Wai%20Yan%20Hein)

---

Top Contributors

[![waiyanhein](https://avatars.githubusercontent.com/u/9372127?v=4)](https://github.com/waiyanhein "waiyanhein (3 commits)")[![waiyanheincyberduck](https://avatars.githubusercontent.com/u/43062909?v=4)](https://github.com/waiyanheincyberduck "waiyanheincyberduck (3 commits)")

### Embed Badge

![Health badge](/badges/waiyanhein-lara-role-manager/health.svg)

```
[![Health](https://phpackages.com/badges/waiyanhein-lara-role-manager/health.svg)](https://phpackages.com/packages/waiyanhein-lara-role-manager)
```

###  Alternatives

[namshi/jose

JSON Object Signing and Encryption library for PHP.

1.8k99.6M101](/packages/namshi-jose)[league/oauth1-client

OAuth 1.0 Client Library

99698.8M106](/packages/league-oauth1-client)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[gesdinet/jwt-refresh-token-bundle

Implements a refresh token system over Json Web Tokens in Symfony

70516.4M35](/packages/gesdinet-jwt-refresh-token-bundle)[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

41721.2M118](/packages/league-oauth2-google)[illuminate/auth

The Illuminate Auth package.

9327.3M1.0k](/packages/illuminate-auth)

PHPackages © 2026

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