Skip to content

命令 ​

创建命令 ​

bash
php artisan make:command OrderRefundCommand

命令实现 ​

php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;


class OrderRefundCommand extends Command
{
    // 命令名称
    public $signature = 'order:refund {orderId}';
    // 命令简介
    public $description = '订单退款';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $orderId = $this->argument('orderId');
        // 执行退款业务
        // ...
        dump($orderId);
    }
}

命令执行 ​

bash
php artisan order:refund 123456

命令参数 ​

php
// 1. 必填位置参数
protected $signature = 'demo:test {id}';
// php artisan demo:test 100

// 2. 可选位置参数(加 ?)
protected $signature = 'demo:test {id?}';

// 3. 参数带默认值
protected $signature = 'demo:test {id=1}';

// 4. 选项(无值) --force
protected $signature = 'demo:test {--force}';

// 5. 选项可接收值
protected $signature = 'demo:test {--limit=}';
// php artisan demo:test --limit=100

// 6. 数组参数(多个值)
protected $signature = 'demo:test {ids*}';
// php artisan demo:test 1 2 3