2

Hi I am new with the use of laravel.I can't understand how to create multiple role user. I want to create an admin and general user . I want to use entrust (https://github.com/Zizaco/entrust) if possible. I am using Laravel 5.4 and install entrust. If possible an small example with explain of entrust will be helpful.

Shafayet Rahat
  • 234
  • 3
  • 13
  • Please check the following answer: https://stackoverflow.com/a/44180000/339367 – Zubair1 Aug 03 '17 at 21:43
  • it is not clear in it. Whether I should create new Role in route or in create and declare in a controller. So better a small example file to file with explanation would be much helpful, still thanks @Zubair1 – Shafayet Rahat Aug 03 '17 at 21:52

1 Answers1

2

Step 1. After creating a project in laravel, open the composer.json and update the require object with entrust like this

"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. Open config/app.php and find providers array and add the following line.

Zizaco\Entrust\EntrustServiceProvider::class,

Below providers array find aliases and add the following 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,
    '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 You are ready to go