daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Apr 26th, 2018 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Database\Seeder\Local;
  4.  
  5. use Illuminate\Database\Seeder;
  6. use App\User;
  7. use App\Role;
  8. use App\Permission;
  9.  
  10. class UsersTableSeeder extends Seeder
  11. {
  12.     /**
  13.      * Run the database seeds.
  14.      *
  15.      * @return void
  16.      */
  17.     public function run()
  18.     {
  19.         $permissions = Permission::default();
  20.         $roles = Role::default();
  21.         $totalRoles = \count($roles);
  22.  
  23.         $totalUsers = $this->askTotalUsers($totalRoles);
  24.  
  25.         foreach ($permissions as $permission) {
  26.             Permission::firstOrCreate([
  27.                 'name' => $permission,
  28.             ]);
  29.         }
  30.  
  31.         foreach ($roles as $role) {
  32.             $role = Role::firstOrCreate([
  33.                 'name' => trim($role),
  34.             ]);
  35.  
  36.             if ($role->name === 'admin') {
  37.                 $role->syncPermissions(Permission::all());
  38.             } else {
  39.                 $role->syncPermissions(Permission::where('name', 'LIKE', 'view_%')->get());
  40.             }
  41.  
  42.             $this->createUser($role, $totalUsers);
  43.         }
  44.     }
  45.    
  46.     private function askTotalUsers($totalRoles): int
  47.     {
  48.         $totalUsers = $this->command->ask('How many users you want to create? Default ' . $totalRoles . '.', $totalRoles);
  49.        
  50.         if ($totalUsers < $totalRoles) $this->askTotalUsers($totalRoles);
  51.        
  52.         return $totalUsers;
  53.     }
  54.  
  55.     private function createUser($role, $total): void
  56.     {
  57.         if ($role->name === 'admin') {
  58.             $user = factory(User::class)->create();
  59.             $user->assignRole($role->name);
  60.  
  61.             $this->command->warn('E-Mail Address:' . $user->email);
  62.             $this->command->warn('Password: secret');
  63.         } else {
  64.             $user = factory(User::class, $total - 1)->create();
  65.             $user->assignRole($role->name);
  66.         }
  67.     }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top