63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace Database\Seeders;
 | 
						|
 | 
						|
use App\Models\User;
 | 
						|
use Illuminate\Database\Seeder;
 | 
						|
use Spatie\Permission\Models\Role;
 | 
						|
 | 
						|
class DatabaseSeeder extends Seeder
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * Seed the application's database.
 | 
						|
     */
 | 
						|
    public function run(): void
 | 
						|
    {
 | 
						|
        // Create roles if they don't already exist
 | 
						|
        $roles = ['admin', 'privileged'];
 | 
						|
    
 | 
						|
        foreach ($roles as $roleName) {
 | 
						|
            // Check if the role already exists
 | 
						|
            if (!Role::where('name', $roleName)->exists()) {
 | 
						|
                Role::create(['name' => $roleName]);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    
 | 
						|
        // Check if the superuser already exists
 | 
						|
        $user = User::firstOrCreate([
 | 
						|
            'email' => 'superuser@admin.com',
 | 
						|
            'name' => 'superuser',
 | 
						|
            'password' => bcrypt('12341234'),
 | 
						|
        ]);
 | 
						|
    
 | 
						|
        // Assign the role to the superuser
 | 
						|
        $user->assignRole('admin');
 | 
						|
    
 | 
						|
        // Create other users as before
 | 
						|
        $users = [
 | 
						|
            ['name' => 'John Doe', 'email' => 'john@doe.com', 'role' => 'privileged'],
 | 
						|
            ['name' => 'Chuck Norris', 'email' => 'chuck@norris.com'],
 | 
						|
            ['name' => 'Marios Bros', 'email' => 'mario@bros.com'],
 | 
						|
            ['name' => 'Ada Lovelace', 'email' => 'ada@lovelace.com'],
 | 
						|
            ['name' => 'Hulk Hogan', 'email' => 'hulk@hogart.com'],
 | 
						|
 | 
						|
        ];
 | 
						|
    
 | 
						|
        foreach ($users as $userData) {
 | 
						|
            $user = User::firstOrCreate([
 | 
						|
                'email' => $userData['email'],
 | 
						|
                'name' => $userData['name'],
 | 
						|
                'password' => bcrypt('12341234'),
 | 
						|
            ]);
 | 
						|
            if (array_key_exists('role', $userData) && $userData['role']) {
 | 
						|
                $user->assignRole($userData['role']);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 |