2

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();
LazyOne
  • 158,824
  • 45
  • 388
  • 391
the inquisitor
  • 99
  • 1
  • 1
  • 4
  • yeah their documentation is not great, you can put this in a controller and run it once to make the roles if you want, you could also do this in artisan – ATechGuy Jan 26 '18 at 18:21

1 Answers1

1

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
gre_gor
  • 6,669
  • 9
  • 47
  • 52
hoseinz3
  • 618
  • 4
  • 13
  • is there any difference between using seeder or a controller that i run only once ? – the inquisitor Jan 26 '18 at 19:09
  • @theinquisitor Not in execution. But with console/terminal you are owner of that process and can not be unintentionally requested by someone over browser. – Tpojka Jan 26 '18 at 22:49