Php 在命令行上运行Laravel任务时如何传递多个参数?

Php 在命令行上运行Laravel任务时如何传递多个参数?,php,laravel,Php,Laravel,我使用需要多个参数的方法创建了一个任务类: class Sample_Task { public function create($arg1, $arg2) { // something here } } 但artisan似乎只得到了第一个论点: php artisan sample:create arg1 arg2 错误消息: Warning: Missing argument 2 for Sample_Task::create() 如何在此方法中传递多个

我使用需要多个参数的方法创建了一个任务类:

class Sample_Task
{
    public function create($arg1, $arg2) {
        // something here
    }
}
但artisan似乎只得到了第一个论点:

php artisan sample:create arg1 arg2
错误消息:

Warning: Missing argument 2 for Sample_Task::create()
如何在此方法中传递多个参数?

Laravel 5.2

class Sample_Task
{
    public function create($args) {
       $arg1 = $args[0];
       $arg2 = $args[1];
        // something here
    }
}
您需要做的是将
$signature
属性中的参数(或选项,例如--option)指定为数组。Laravel用星号表示这一点

参数

e、 g.假设您有Artisan命令“处理”图像:

如果你这样做:

php artisan help image:process
…Laravel将负责添加正确的Unix样式语法:

Usage:
  image:process <id> (<id>)...
选项

我说过它也适用于选项,您可以在
$signature
中使用
{--id=*}

帮助文本将显示:

Usage:
  image:process [options]

Options:
      --id[=ID]         (multiple values allowed)
  -h, --help            Display this help message

  ...
因此,用户将键入:

php artisan image:process --id=1 --id=2 --id=3
要访问
handle()
中的数据,可以使用:

$ids = $this->option('id');
如果省略'id',您将获得所有选项,包括表示'quiet'的布尔值、“verbose”等

$options = $this->option();
您可以访问
$options['id']

更多信息请访问

$ids = $this->option('id');
$options = $this->option();