Implement roles and permissions on your laravel application, supports many-to-many relationship (pivot tables).
ajimoti/roles-and-permissions is a Laravel package for implement roles and permissions on your laravel application, supports many-to-many relationship (pivot tables)..
It currently has 62 GitHub stars and 1.304 downloads on Packagist (latest version 1.1.2).
Install it with composer require ajimoti/roles-and-permissions.
Discover more Laravel packages by ajimoti
or browse all Laravel packages to compare alternatives.
Last updated

This package allows you to assign roles and permissions to any laravel model, or on a pivot table (many to many relationship).
Built and written by Ajimoti John
You can read the proper documentation here
The example below explains how to use the package on a model after installation.
use App\Enums\Role;
use App\Enums\Permission;
// Assign a 'Super Admin' role to this user
$user->assign(Role::SuperAdmin);
// Check if the user has the role
$user->hasRole(Role::SuperAdmin);
// Or
$user->isSuperAdmin(); // returns true
// Check if the user can perform a operation
$user->can(Permission::DeleteTransactions);
// Or
$user->canDeleteTransactions();
// Check if the user has multiple permissions
$user->holds(Permission::DeleteTransactions, Permission::BlockUsers);
This demonstrates how to use the package on a many to many relationship.
In this example, we assume we have a merchant relationship in our User model. And this relationship returns an instance of Laravel's BelongsToMany class.
Import the App\Enums\Role and App\Enums\Permission class.
use App\Enums\Role;
use App\Enums\Permission;
// Sample merchant
$merchant = Merchant::where('name', 'wallmart')->first();
// Assign a 'Super Admin' role to this user on the selected merchant (wallmart)
$user->of($merchant)->assign(Role::SuperAdmin);
// Check if the user has a super admin role on the selected merchant (wallmart)
$user->of($merchant)->hasRole(Role::SuperAdmin);
// Check if the user can 'delete transactions' on the selected merchant (wallmart)
$user->of($merchant)->can(Permission::DeleteTransactions);
// Check if the user has multiple permissions on the selected merchant (wallmart)
$user->of($merchant)->holds(Permission::DeleteTransactions, Permission::BlockUsers);
We used the
usermodel to make the example explanatory, similar to the examples above the package will work on any model class.
config/roles-and-permissions.php file, ensure you do not have a file with the same name in your config directory.many to many relationship can be assigned roles. (i.e the package can be used on a pivot table).many to many relationship.You can install the package via composer:
composer require ajimoti/roles-and-permissions
If you have existing pivot tables that you want to apply the package on, you can add the table names to the pivot.tables array in the config/roles-and-permissions.php config file. The command below will add a role column to every pivot table provided in the array.
Run the command below, then you are set to use the package.
php artisan roles:install
Visit documentation here to better understand how to use the package.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.