// 参数
// protected $signature = 'test:send {name}'; // 必填参数
// protected $signature = 'test:send {arg?}'; //必填参数
// protected $signature = 'test:send {name=test}'; // 默认参数
// protected $signature = 'test:send {name*}'; // 数组参数
// 选项
// protected $signature = 'test:send {--id}'; // 不接受值的选项
// protected $signature = 'test:send {--id=}'; // 需要接收值的选项
// protected $signature = 'test:send {--id=0}'; // 选项默认值
// protected $signature = 'test:send {--id=*}'; // 选项数组
protected $signature = 'test:send {name=test} {--id=}';
* The console command description.
* @var string
protected $description = 'A test command';
* Create a new command instance.
* @return void
public function __construct()
parent::__construct();
* Execute the console command.
* @return mixed
public function handle()
$name = $this->ask('Your username is?');
$this->info('Your name is:'. $name);
$password = $this->secret('Your password is?');
$this->info('Your password is:'.$password);
$continue = $this->confirm('Do you want to continue?');
if ($continue) {
$this->info('yes, continue');
} else {
$this->warn('no, break');
$city = $this->choice('Your city is?', ['Beijing', 'Shanghai', 'Shenzhen']);
$this->info($city);
$this->line('默认字体颜色输出');
$this->info('绿色字体颜色输出');
$this->comment('棕色字体颜色输出');
$this->question('字体蓝色背景输出');
$headers = ['name', 'password', 'city'];
$users = [
'name' => $name,
'password' => $password,
'city' => $city
$this->table($headers, $users);
// 获取指定参数
if ($this->hasArgument('name')) {
// 获取所有参数 $args = $this->arguments()
$arg = $this->argument('name');
if (!is_null($arg)) {
$this->info($arg);
// 获取指定选项
if ($this->hasOption('id')) {
// 获取所有选项 $options = $this->options()
$option = $this->option('id');
if (!is_null($option)) {
$this->info($option);
3.在app\Console\Kernel.php中添加命令
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
* The Artisan commands provided by your application.
* @var array
protected $commands = [
// Commands\Inspire::class,
Commands\Test::class
* Define the application's command schedule.
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
protected function schedule(Schedule $schedule)
// $schedule->command('inspire')
// ->hourly();
4. 控制台测试
php artisan test:send testc --id=2