entrust laravel is a succinct and flexible way to add Role-based Permissions to Laravel web application. How to implement Entrust in Laravel?
User ACL Roles and Permissions with Middleware using entrust Laravel
first of all i know to you entrust laravel package supports a flexible methods to add Role-based Permissions to your PHP Laravel web based application.
I like my custom add roles and permissions to Laravel solution, but I figured there had to be a better solution out there using zizaco entrust laravel.
also It’s a This package makes 4 Database tables:
- Roles table: It’s gathering role records
- Role_user table: It’s gathering one-to-many relations between roles as well as users
- Permissions table: It’s gathering permission records
- Permission_role table: It’s gathering many-to-many relations between roles as well as permissions
how to implement entrust in laravel?
"zizaco/entrust": "5.2.x-dev"
Also Read : php user role management system
Step 1: Install Entrust
After creating a web project in laravel web apps, and the you can open the main file composer.json and change or update the require some object with entrust like this Example
"require": { "php": ">=7.1.3", "fideloper/proxy": "~4.0", "laravel/framework": "5.5.*", "laravel/tinker": "~1.0", "tymon/jwt-auth": "1.0.0-rc.1", "zizaco/entrust": "dev-master" },
Then Run
composer update
Step 2: Add Entrust Provider and Facades
Now you can simply Open config/app.php and find here providers array and include the following php class line.
Zizaco\Entrust\EntrustServiceProvider::class,
Below providers array find aliases and add the bellow line
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
Then Run this command
php artisan vendor:publish
After this you will see a new file in config directory named entrust.php
Step 3. Open app/Http/Kernel.php and add the middleware
<!--?php /** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ .... 'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,<br ?--> 'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class, 'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class, ]; ?>
Step 4. Run This
php artisan entrust:migration
Above command will create the 4 tables roles, permissions, role_user and permission_role
Now simple step by step run your laravel application. You are ready to go zizaco entrust laravel