laravel-entrust maintained by odhiamboatieno
Laravel Entrust (Supports Laravel 5 to 12)
A robust and flexible package to manage role-based permissions for your Laravel applications.
Version Compatibility
| Laravel | Laravel Entrust |
|---|---|
| 12.x | [4.x] |
| 11.x | [4.x] |
| 10.x | [4.x] |
| 9.x | [3.x] |
| 8.x | [2.x] |
| 7.x - 5.x | [1.x] |
📌 Table of Contents
🚀 Introduction
Laravel Entrust is a role and permission management package for Laravel applications, providing an intuitive way to manage user roles and permissions.
Key Features
- Role and Permission Management
- Middleware for Authorization Control
- Configurable Permission Structure
- Easy-to-use API for Role and Permission Checks
✅ Installation
- Install the package via composer:
composer require odhiamboatieno/laravel-entrust
- Publish the package configuration:
php artisan vendor:publish --tag="LaravelEntrust"
- Run the setup command to create migration files:
php artisan laravel-entrust:setup
- Add the
LaravelEntrustUserTraitto your User model:
use Odhiambo\LaravelEntrust\Traits\LaravelEntrustUserTrait;
class User extends Model
{
use LaravelEntrustUserTrait;
}
⚙️ Configuration
-
Configure the published file in
config/entrust.phpto set your models, tables, and guard settings. -
Define the roles, permissions, and relationships as needed for your application.
✅ Usage
Roles and Permissions
Assign roles and permissions to users using the trait methods:
$user = User::find(1);
$user->attachRole('admin');
$user->hasRole('admin'); // true
$user->hasPermission('create-post');
Middleware
Protect your routes using middleware for roles or permissions:
Route::group(['middleware' => ['role:admin']], function() {
Route::get('/dashboard', [DashboardController::class, 'index']);
});
Route::get('/posts', [PostController::class, 'index'])->middleware('permission:view-posts');
Soft Deletes
If using soft deletes on roles or permissions, ensure to clear relationships before force deleting:
$role = Role::find(1);
$role->users()->sync([]);
$role->permissions()->sync([]);
$role->forceDelete();
❓ Troubleshooting
If you encounter migration errors, ensure your database uses the correct integer type (BigInteger) for foreign keys.
💡 Contribution Guidelines
- Follow PSR-12 coding standards.
- Submit bug reports or feature requests via GitHub Issues.
- Pull requests are welcome.
📜 License
Laravel-Entrust is licensed under the MIT license.