Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use App\User;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Validation\Rule;
  9. use Illuminate\Support\Facades\Hash;
  10.  
  11. class CreateUser extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'user:create {login} {password} {role}';
  19.  
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26.  
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36.  
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $validator = Validator::make($this->arguments(), [
  45. 'login' => 'required|unique:users',
  46. 'password' => 'required|min:6|max:20',
  47. 'role' => [
  48. 'required',
  49. Rule::in(['admin', 'operator']),
  50. ],
  51. ]);
  52.  
  53. if($validator->fails()){
  54. $this->error($validator->errors()->toJson());
  55. } else {
  56. $user = User::create([
  57. 'phone_number' => "no phone_number",
  58. 'login' => $this->argument('login'),
  59. 'password' => Hash::make($this->argument('password')),
  60. ]);
  61. $user->assignRole($this->argument('role'));
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement