Lae/app/Console/Commands/Admin/Create.php

59 lines
1.3 KiB
PHP
Raw Normal View History

2022-11-14 11:45:48 +00:00
<?php
2022-11-17 11:52:23 +00:00
namespace App\Console\Commands\Admin;
2022-11-14 11:45:48 +00:00
use App\Models\Admin;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
2022-11-17 11:52:23 +00:00
class Create extends Command
2022-11-14 11:45:48 +00:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2022-11-17 11:52:23 +00:00
protected $signature = 'admin:create';
2022-11-14 11:45:48 +00:00
/**
* 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('两次输入的密码不一致。');
2023-01-30 16:14:07 +00:00
2022-11-14 11:45:48 +00:00
return CommandAlias::FAILURE;
}
// 创建管理员
2023-01-10 13:42:27 +00:00
$admin = (new Admin)->create([
2022-11-14 11:45:48 +00:00
'email' => $email,
2023-01-30 16:14:07 +00:00
'password' => bcrypt($password),
2022-11-14 11:45:48 +00:00
]);
// 输出信息
2023-02-07 09:04:11 +00:00
$this->info('管理员创建成功ID为'.$admin->id);
2022-11-14 11:45:48 +00:00
return CommandAlias::SUCCESS;
}
}