diff --git a/app/Console/Commands/UserCreate.php b/app/Console/Commands/UserCreate.php new file mode 100644 index 00000000..e749ca7a --- /dev/null +++ b/app/Console/Commands/UserCreate.php @@ -0,0 +1,41 @@ +makeFaker(); + + /** @var User $newUser */ + $newUser = User::factory()->create([ + 'name' => $this->option('name') ?? $faker->name, + 'email' => $this->option('email') ?? $faker->email, + 'password' => Hash::make($this->option('password') ?? 'password'), + ]); + + $newUser->assignRole($this->option('role')); + + $this->line('User created: '); + $this->line($newUser->toJson()); + + return self::SUCCESS; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index c16f7458..a1375fce 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; class UserFactory extends Factory @@ -16,7 +17,7 @@ public function definition() 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), - 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'password' => Hash::make('password'), 'remember_token' => Str::random(10), ]; } diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php index 07697b3e..e7611558 100644 --- a/database/seeders/UserSeeder.php +++ b/database/seeders/UserSeeder.php @@ -9,32 +9,25 @@ class UserSeeder extends Seeder { public function run(): void { - // `password` - $password = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'; - $factoryUsers = [ [ 'name' => 'Admin', 'email' => 'admin@2023.laravelconf.tw', - 'password' => $password, 'role' => 'admin' ], [ 'name' => 'Miles', 'email' => 'miles@2023.laravelconf.tw', - 'password' => $password, 'role' => 'shipper' ], [ 'name' => 'Nathan', 'email' => 'nathan@2023.laravelconf.tw', - 'password' => $password, 'role' => 'user' ], [ 'name' => 'Ban', 'email' => 'ban@2023.laravelconf.tw', - 'password' => $password, 'role' => 'user' ], ];