odhiamboatieno/laravel-entrust

This package provides a flexible solution to add ACL to Laravel

dev-main 2025-05-10 13:34 UTC

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

  1. Install the package via composer:
composer require odhiamboatieno/laravel-entrust
  1. Publish the package configuration:
php artisan vendor:publish --tag="LaravelEntrust"
  1. Run the setup command to create migration files:
php artisan laravel-entrust:setup
  1. Add the LaravelEntrustUserTrait to your User model:
use Odhiambo\LaravelEntrust\Traits\LaravelEntrustUserTrait;

class User extends Model
{
    use LaravelEntrustUserTrait;
}

⚙️ Configuration

  1. Configure the published file in config/entrust.php to set your models, tables, and guard settings.

  2. 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.