Laravel 5 将数据从控制器传递到命令

Laravel 5 pass data from controller to command

我正在尝试将一个数组从我的控制器传递到我的命令。请参阅下面的代码

Queue::push(new SendReminderPush(),array('data' => $data));

但是当我调用命令时,我总是得到一个异常。

App\Commands\SendReminderPush::handle()

缺少参数 1

这是我在命令中的句柄函数 class:

public function handle($data){
   foreach($data as $d){
    do something
   }
}

请帮助我。我做错了什么?

在Laravel 5中这实际上取决于$data是什么。如果它是一个数组并且你想让 Laravel 自动映射它,你可以这样做:

$this->dispatchFromArray('App\Commands\SendReminderPush', $data);

说你的 $data 也像这样:

$data = array('name' => 'Test', 'email' => 'test@example.com');

在您的 SendReminderPush 中,您可以将其映射到构造函数中:

public function __construct($name, $email) {
   $this->name = $name;
   $this->email = $email;
}

然后您将在命令中处理它(如果它是一个自处理命令),如下所示:

public function handle(){
   $this->doSomething($this->name);
}

我会进一步了解命令总线在 Laravel 5 中的工作原理。看看here