无法为自定义命令添加参数

问题描述 投票:3回答:2

我正在使用laravel 5.1。在debian bash shell中。我创建了一个名为survey:complete的自定义控制台命令。我已经使用了一段时间了,现在我想为要生成的调查数量添加一个可选参数。

但是,我已经按照文档,但我无法成功添加我的论点。我同样改变了签名:

protected $signature = 'survey:complete {--number=}';

并试图引用这个论点

public function handle() { 
    for( $i = 0; $i < $this->argument('number'); $i++ ) { 

但我得到这个错误:

$> php artisan survey:complete --number=1
[InvalidArgumentException]
The "number" argument does not exist.

我print_r()'参数数组,我得到这个:

$ php artisan survey:complete --number=1
Array(
    [command] => survey:complete
)
[InvalidArgumentException]
The "number" argument does not exist.

如何将我的参数添加到我的命令中?

laravel-5 laravel-5.1
2个回答
5
投票

我需要使用option(),而不是argument()

$number = $this->option('number');

0
投票

为我而工作:

   protected $signature = 'mycommand {limit=1000}'
   // call: php artisan mycommand 100

我用以下方法检索值:

public function handle() {    
    // ...
    $limit = $this->arguments('limit')['limit']
    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.