DEV Community

Cover image for Using Laratrust Permission and Roles Other Than Users Relation
Uzair Khan
Uzair Khan

Posted on

Using Laratrust Permission and Roles Other Than Users Relation

Laratrust is a laravel package that lets you handle very easily roles and permission inside your application.
laratrust by default make use of users relation but sometimes you wish not to use users relation in order to do so there are few steps to follow.

step 1: install the package using composer.

   composer require santigarcor/laratrust
Enter fullscreen mode Exit fullscreen mode

step 2: public the configuration files.

   php artisan vendor:publish --tag="laratrust"
Enter fullscreen mode Exit fullscreen mode

step: 3 Now before setting up laratrust you have to make a few changes to config/laratrust.php in laratust.php file make changes to tables array like so

    'tables' => [
        'roles' => 'roles',
        'permissions' => 'permissions',
        'teams' => 'teams',
        'role_user' => 'role_admin',
        'permission_user' => 'permission_admin',
        'permission_role' => 'permission_role',
     ],
Enter fullscreen mode Exit fullscreen mode

Note: do not change keys of tables array let it be as it is.

step: 4 And now a slight change to the foreign keys array instead of using user_id it should now be admin_id like so.

         'foreign_keys' => [
               'user' => 'admin_id',
               'role' => 'role_id',
               'permission' => 'permission_id',
               'team' => 'team_id',
              ],
Enter fullscreen mode Exit fullscreen mode

now you are all set to use laratrust with admin table instead of users table.

Run the following artisan command to setup your laratrust migration.

 php artisan laratrust:setup
Enter fullscreen mode Exit fullscreen mode

in migration of laratrust you would be able to see that migration is setup for admin table now simply migrate using.

 php artisan migrate
Enter fullscreen mode Exit fullscreen mode

and that’s all for using laratrust with a different relation than users.

For further details please go to laratrust official site https://laratrust.santigarcor.me

Thank you

Top comments (0)