laravel-5.4entrust

how to implement entrust in laravel 5.4


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.


Solution

  • 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