Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\User;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Validation\Rule;
- use Illuminate\Support\Facades\Hash;
- class CreateUser extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'user:create {login} {password} {role}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $validator = Validator::make($this->arguments(), [
- 'login' => 'required|unique:users',
- 'password' => 'required|min:6|max:20',
- 'role' => [
- 'required',
- Rule::in(['admin', 'operator']),
- ],
- ]);
- if($validator->fails()){
- $this->error($validator->errors()->toJson());
- } else {
- $user = User::create([
- 'phone_number' => "no phone_number",
- 'login' => $this->argument('login'),
- 'password' => Hash::make($this->argument('password')),
- ]);
- $user->assignRole($this->argument('role'));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement