PHPackages                             shanmuga/laravel-entrust - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. shanmuga/laravel-entrust

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

shanmuga/laravel-entrust
========================

This package provides a flexible solution to add ACL to Laravel

v6.0(1y ago)68312.9k↑25%18[1 PRs](https://github.com/shanmuga3/laravel-entrust/pulls)2MITPHPPHP ^8.1

Since Jan 26Pushed 1y ago2 watchersCompare

[ Source](https://github.com/shanmuga3/laravel-entrust)[ Packagist](https://packagist.org/packages/shanmuga/laravel-entrust)[ RSS](/packages/shanmuga-laravel-entrust/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (21)Used By (2)

Laravel Entrust (Supports Laravel 5 to 10)
==========================================

[](#laravel-entrust-supports-laravel-5-to-10)

Handle Role-based Permissions for your Laravel application.

Version Compatibility
---------------------

[](#version-compatibility)

LaravelLaravel Entrust10.x\[4.x\]9.x\[3.x\]8.x\[2.x\]7.x - 5.x\[1.x\]Contents
--------

[](#contents)

- [Installation &amp; Configuration](#installation)
- [Usage](#usage)
    - [Concepts](#concepts)
        - [Checking for Roles &amp; Permissions](#checking-for-roles--permissions)
        - [User ability](#user-ability)
    - [Middleware](#middleware)
- [Troubleshooting](#troubleshooting)
- [License](#license)
- [Contribution guidelines](#contribution-guidelines)
- [Additional information](#additional-information)

Installation
------------

[](#installation)

1. You can install the Laravel-entrust package via composer:

```
composer require shanmuga/laravel-entrust
```

> **Note:** You Can Skip step 2 and 3 If you are using above Laravel 5.5.

2. Open your `config/app.php` and add the following to the `providers` array:

```
Shanmuga\LaravelEntrust\LaravelEntrustServiceProvider::class,
```

3. In the same `config/app.php` and add the following to the `aliases ` array:

```
'LaravelEntrust'   => Shanmuga\LaravelEntrust\Facades\LaravelEntrustFacade::class,
```

4. Run the command below to publish the package config files `config/entrust.php` and `config/entrust_seeder.php`

```
php artisan vendor:publish --tag="LaravelEntrust"
```

5. Open your `config/entrust.php` and add the following to it:

Name of the migration file to be generated

```
'migrationSuffix' => 'laravel_entrust_setup_tables',
```

Model and Table Used for Authorization

```
'user_model' => 'App\Models\User',
'user_table' => 'users',
```

Name of the Models Used for Role and Permission

```
'models' => [
    'role'          => 'App\Models\Role',
    'permission'    => 'App\Models\Permission',
],
```

Default Guard to perform user authentication, You Can also pass it manually when checking it.

```
'defaults' => [
     'guard'          => 'web',
 ],
```

You can also use multiple guards:

```
'defaults' => [
     'guard'          => ['web', 'api'],
 ],
```

Table names used for roles and permissions

```
'tables' => [
    'roles'             => 'roles',
    'permissions'       => 'permissions',
    'role_user'         => 'role_user',
    'permission_role'   => 'permission_role',
],
```

Foriegn keys used for roles and permissions

```
'foreign_keys' => [
    'user' => 'user_id',
    'role' => 'role_id',
    'permission' => 'permission_id',
],
```

Middleware Setup for custom message, register set to true for register automatically, **Handling** is which handler to be used either *abort* or *redirect*. you can also configure what message should be display if authorization failed.

```
'middleware' => [
    'register' => true,
    'handling' => 'abort',
    'handlers' => [
        'abort' => [
            'code' => 403,
            'message' => 'You don\'t Have a permission to Access this page.'
        ],
        'redirect' => [
            'url' => '/',
            'message' => [
                'key' => 'error',
                'content' => 'You don\'t Have a permission to Access this page'
            ]
        ],
    ],
],
```

6. Run the following command to generate migration and seed

```
php artisan laravel-entrust:setup
```

> See [Entrust Seeder](#entrust_seeder) Configuration to learn more about create permissions.

7. Finally Add the LaravelEntrustUserTrait to existing `User` model. For example:

```
