Skip to content

Commit

Permalink
Add create user command
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Jul 12, 2023
1 parent 9f58877 commit c0521b2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
41 changes: 41 additions & 0 deletions app/Console/Commands/UserCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Hash;

class UserCreate extends Command
{
use WithFaker;

protected $signature = 'app:user:create
{--name=}
{--email=}
{--password=}
{--role=user}
';

protected $description = 'Create User';

public function handle(): int
{
$faker = $this->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;
}
}
3 changes: 2 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
];
}
Expand Down
7 changes: 0 additions & 7 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'password' => $password,
'role' => 'admin'
],
[
'name' => 'Miles',
'email' => '[email protected]',
'password' => $password,
'role' => 'shipper'
],
[
'name' => 'Nathan',
'email' => '[email protected]',
'password' => $password,
'role' => 'user'
],
[
'name' => 'Ban',
'email' => '[email protected]',
'password' => $password,
'role' => 'user'
],
];
Expand Down

0 comments on commit c0521b2

Please sign in to comment.