Using Laratrust Permission and Roles Other Than Users Relation

Image for post

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 the composer.

composer require santigarcor/laratrust

step 2: publish the configuration files.

php artisan vendor:publish --tag="laratrust"

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',
/**
* Will be used only if the team's functionality is enabled.
*/
'teams' => 'teams',
'role_user' => 'role_admin',
'permission_user' => 'permission_admin',
'permission_role' => 'permission_role',
],

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',
],

now you are all set to use laratrust with the admin table instead of the users table. Run the following artisan command to set up your laratrust migration.

php artisan laratrust:setup

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

php artisan migrate

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

For further details please go to laratrust official site.