Search code examples
phplaravelentrust

Location of entrust roles and permissions code


I am new to Laravel and trying to use enrtust plugin for roles and permissions, but when following the instructions in the documentation, I reached the bit where I have to put the code that creates the roles and permissions, but it did not mention where I have to put it. What is the best place to put it?

Here is the code I am talking about:

$owner = new Role();
$owner->name         = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description  = 'User is the owner of a given project'; // 
$owner->save();

$admin = new Role();
$admin->name         = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description  = 'User is allowed to manage and edit other users'; 
$admin->save();

Solution

  • I usually use seeder for this situation. In this example I create RoleTableSeeder, PermissionTableSeeder and PermissionRoleTableSeeder.

    You can run seeder separately with this command:

    php artisan db:seed --class=PermissionTableSeeder
    

    or you can run all seeder by this command:

    php artisan db:seed