Lae/app/Console/Commands/CreateAdmin.php

60 lines
1.3 KiB
PHP
Raw Normal View History

2022-11-14 11:45:48 +00:00
<?php
namespace App\Console\Commands;
use App\Models\Admin;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
class CreateAdmin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'create:admin';
/**
* The console command description.
*
* @var string
*/
protected $description = '创建一个管理员账号';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
// 邮箱
$email = $this->ask('请输入邮箱');
// 密码
$password = $this->secret('请输入密码');
// 确认密码
$password_confirmation = $this->secret('请再次输入密码');
// 验证密码
if ($password !== $password_confirmation) {
$this->error('两次输入的密码不一致。');
return CommandAlias::FAILURE;
}
// 创建管理员
$admin = Admin::create([
'email' => $email,
'password' => bcrypt($password)
]);
// 输出信息
$this->info('管理员创建成功ID为' . $admin->id);
return CommandAlias::SUCCESS;
}
}