|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Devpri\Tinre\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Devpri\Tinre\Models\User; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\Hash; |
|
8
|
|
|
use Illuminate\Support\Facades\Validator; |
|
9
|
|
|
|
|
10
|
|
|
class CreateUser extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'user:create'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Create User'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new command instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
167 |
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
167 |
|
parent::__construct(); |
|
34
|
167 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Execute the console command. |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function handle() |
|
42
|
|
|
{ |
|
43
|
1 |
|
$name = $this->ask('Name?'); |
|
44
|
|
|
|
|
45
|
1 |
|
$email = $this->ask('Email?'); |
|
46
|
|
|
|
|
47
|
1 |
|
$password = $this->ask('Password?'); |
|
48
|
|
|
|
|
49
|
1 |
|
$role = $this->choice('Role?', config('tinre.roles')); |
|
50
|
|
|
|
|
51
|
1 |
|
$validator = Validator::make([ |
|
52
|
1 |
|
'name' => $name, |
|
53
|
1 |
|
'email' => $email, |
|
54
|
1 |
|
'password' => $password, |
|
55
|
1 |
|
'role' => $role, |
|
56
|
|
|
], [ |
|
57
|
1 |
|
'name' => ['required'], |
|
58
|
|
|
'email' => ['required', 'email', 'unique:users'], |
|
59
|
|
|
'password' => ['required', 'min:8'], |
|
60
|
|
|
'role' => ['required'], |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
1 |
|
if ($validator->fails()) { |
|
64
|
|
|
$this->info('User not created!'); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($validator->errors()->all() as $error) { |
|
67
|
|
|
$this->error($error); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return 1; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$data = $validator->valid(); |
|
74
|
|
|
|
|
75
|
1 |
|
User::create([ |
|
76
|
1 |
|
'name' => $data['name'], |
|
77
|
1 |
|
'email' => $data['email'], |
|
78
|
1 |
|
'role' => $data['role'], |
|
79
|
1 |
|
'password' => Hash::make($data['password']), |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
1 |
|
$this->info('User created!'); |
|
83
|
1 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|