我创建了一个带有需要多个参数的方法的Task类:
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
你需要做的是在$signature
属性中指定参数(或选项,例如——option)作为数组。Laravel用星号表示。
。假设你有一个Artisan命令来"处理"图像:
protected $signature = 'image:process {id*}';
如果你这样做:
php artisan help image:process
…Laravel将负责添加正确的unix风格语法:
Usage:
image:process <id> (<id>)...
在handle()
方法中访问列表,只需使用:
$arguments = $this->argument('id');
foreach($arguments as $arg) {
...
}
选项> 我说它也适用于选项,你在$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']
class Sample_Task
{
public function create($args) {
$arg1 = $args[0];
$arg2 = $args[1];
// something here
}
}
也可以运行传递属性:
php artisan image:process {id=1} {id=2} {id=3}