AdminRolePermissionTableSeeder.php 891 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. use App\Models\AdminPermission;
  3. use App\Models\AdminRole;
  4. use Illuminate\Database\Seeder;
  5. class AdminRolePermissionTableSeeder extends Seeder
  6. {
  7. /**
  8. * Run the database seeds.
  9. *
  10. * @return void
  11. */
  12. public function run()
  13. {
  14. $roles = AdminRole::all();
  15. if ($roles->isEmpty()) {
  16. $this->call(AdminRolesTableSeeder::class);
  17. $roles = AdminRole::all();
  18. }
  19. $perms = AdminPermission::pluck('id');
  20. if ($perms->isEmpty()) {
  21. $this->call(AdminPermissionsTableSeeder::class);
  22. $perms = AdminPermission::pluck('id');
  23. }
  24. $faker = app(Faker\Generator::class);
  25. $roles->each(function (AdminRole $role) use ($faker, $perms) {
  26. $role->permissions()
  27. ->attach($faker->randomElements($perms, $faker->numberBetween(0, 5)));
  28. });
  29. }
  30. }