How to use laratrust in Laravel (ACL) [ Updated 2019]

Hi guys in this post we are going to add user roles and permissions using laratrust package (ACL) in our laravel application. We alreay setup laratrust package installation in another post you should check that first.

Now we are going to seed roles to the database. Once roles are added we are going to attach them with users.

Let's Create Roles Seeder

So let's create "RolesTableSeeder" using below given command.


php artisan make:seeder RolesTableSeeder

It should add a file under

database/seeds/RolesTableSeeder.php

Open this file in a text editor. And add below-given code in this file.

use Illuminate\Database\Seeder;
use App\Role; //added role model


class RolesTableSeeder extends Seeder {

    /**
    * Run the database seeds.
    *
    * @return void
    */


    public function run()
    {
        //create admin role
        $admin = new Role;     //creating role using role model
        $admin->name = "admin";
        $admin->display_name = "Admin";
        $admin->save();
        
        //create editor role
        $editor = new Role; //creating role using role model
        $editor->name = "editor";
        $editor->display_name = "editor";
        $editor->save();


        //create author role
        $author = new Role; //creating role using role model
        $author->name = "author";
        $author->display_name = "author";
        $author->save();
    }
}

Now we need to register "RolesTableSeeder". To do that

go to "database/seeds/DatabaseSeeder"

Here add this line


call(RolesTableSeeder::class); //here registering the seeder
}
}

We have registered a seeder. Using this seeder we can add roles to the roles database table. To do that run below given command.

php artisan db:seed

Should look like this

How to use laratrust in Laravel (ACL) [ Updated 2019]

In your database, you should have roles inserted as given below

How to use laratrust in Laravel (ACL) [ Updated 2019]

In Our "user" database table we have 3 users. we are going to attach these roles to each user.


How to use laratrust in Laravel (ACL) [ Updated 2019]

Now, we get each user by "ID" and we attach the roles.

It can be done by this. We need to add more lines of code in our RolesTableSeeder as given below.

use Illuminate\Database\Seeder;
use App\Role;
use App\User;
class RolesTableSeeder extends Seeder
{
        /**
            * Run the database seeds.
            *
            * @return void
            */
        public function run()
        {
            //reset roles table
            //DB::table("roles")->truncate();
            DB::table('roles')->delete();
            //create admin role
            $admin = new Role;
            $admin->name = "admin";
            $admin->display_name = "Admin";
            $admin->save();
            //create editor role
            $editor = new Role;
            $editor->name = "editor";
            $editor->display_name = "editor";
            $editor->save();
            //create author role
            $author = new Role;
            $author->name = "author";
            $author->display_name = "author";
            $author->save();
            //attach roles to users

            //first user as admin
            $user1 = User::find('1');
            //get user where id is 1
            $user1->detachRole($admin); 
            //detach role so that we wont get duplicate entry error if we run seeder again 

            $user1->attachRole($admin); 
            //attaching role here //second user as editor 
            $user2 = User::find('2'); 
            //get user where id is 2 
            $user2->detachRole($editor); 
            //detach role so that we wont get duplicate entry error if we run seeder again 

            $user2->attachRole($editor);
            //attaching role here 
            //third user as author 
            $user3 = User::find('3'); 
            //get user where id is 3 
            $user3->detachRole($author); 
            //detach role so that we wont get duplicate entry error if we run seeder again 
            $user3->attachRole($author); //attaching role here
                                                                                                                                                                                   
                                                                                                                                                                                            }
                                                                                                                                                                                    
                                                                                                                                                                                    }

For finalizing attachment of roles we need to run seed command one more time.

php artisan db:seed

Roles are attached.

Now if you want to test it

You can Like this


$user1 = App\User::find'(1'); 
$user1->hasRole('admin'); //this should return boolean value if "admin" then true otherwise false

We have added few links to admin page(home page by default in laravel project).

Code looks like this now

@extends('layouts.app')

@section('content')
    Dashboard
    Author One
    Author One
    Editor One
    Editor One
@role(['admin'])
    Admin One
    Admin One
@endrole

@if (session('status'))
    {{ session('status') }}
@endif
You are logged in!
@endsection

So here using "$role" we are checking if the user should see content or not. Link "Admin One" will be visible to admin only.

In the above code

user3 has role "Author" (user name is Smith)

So user 3 is not admin, that's why he cannot see admin links, As given in the image below

How to use laratrust in Laravel (ACL) [ Updated 2019]

user1 has role "Admin" (user name is Manu)

If user1 logged in all links will be visible to him as user1 is admin and he has full access.

How to use laratrust in Laravel (ACL) [ Updated 2019]