lewis15520 / lararoles
A roles and permissions system for laravel
Installs: 961
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
pkg:composer/lewis15520/lararoles
Requires
- laravel/framework: ^11.0
This package is auto-updated.
Last update: 2025-12-20 01:56:02 UTC
README
A package to add roles and permissions to laravel
Requirements
- PHP 7.3 or later
- Laravel 8 or later
Installation
In order to run Lararoles you are required to follow these steps in your terminal
- Install the package with
composer require lewis15520/lararoles - Copy the required package conents with
php artisan vendor:publish --provider="Lewis15520\Lararoles\app\Providers\LararolesServiceProvider" - Install the package migrations with
php artisan migrate
Usage
Adding the trait
In your User model, add the following lines in the use cases: use Lewis15520\Lararoles\Traits;. Then, inside the class, above the functions, add the following: use Lararoles;
Checking the roles and permissions
Anywhere around the application where you have a user object (including from the auth()->user() helper), you can add attach a series of functions to check roles and permissions.
-
hasRole: Usage "
$user->hasRole('roleName');" | Definition "This will check for a singular role attached to this user." -
hasRoles: Usage "
$user->hasRoles(['role1Name', 'role2Name'], (optional) $requireAll = false);" | Definition "This will check if atleast one of the roles given is attached to the user, unless the$requireAllvariable is set totrue, then it will only pass if all the roles are attached to the user." -
hasPermission: Usage "
$user->hasPermission('permissionName');" | Definition "This will check for a singular permission attached to this user through their attached roles." -
hasPermissions: Usage "
$user->hasPermissions(['permission1Name', 'permission2Name'], (optional) $requireAll = false);" | Definition "This will check if atleast one of the permissions given is attached to one of the roles that the user is assigned to, unless the$requireAllvariable is set totrue, then it will only pass if all the permissions are associated."