odhiamboatieno / laravel-entrust
This package provides a flexible solution to add ACL to Laravel
Requires
- php: ^8.1
- illuminate/cache: ^10.0|^11.0|^12.0
- illuminate/config: ^10.0|^11.0|^12.0
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5.10|^10.5
This package is not auto-updated.
Last update: 2025-06-22 11:36:23 UTC
README
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
LaravelEntrustUserTrait
to your User model:
use Odhiambo\LaravelEntrust\Traits\LaravelEntrustUserTrait; class User extends Model { use LaravelEntrustUserTrait; }
⚙️ Configuration
-
Configure the published file in
config/entrust.php
to 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.