laravel 如何将参数发送到队列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32857298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send parameters to queues?
提问by PHPst
Please consider the following job:
请考虑以下工作:
<?php
namespace App\Jobs;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ImportUsers extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function __construct($number)
{
$this->number=$number;
}
public function handle()
{
dd($this->number);
return;
}
}
Dispatching this job using a sync
queue $this->dispatch(new \App\Jobs\ImportUsers(5));
throw this exception: Undefined property: App\Jobs\ImportUsers::$number
. This really seems odd for me. Why the handle method can not access class properties?
使用sync
队列分派此作业会$this->dispatch(new \App\Jobs\ImportUsers(5));
引发此异常:Undefined property: App\Jobs\ImportUsers::$number
。这对我来说真的很奇怪。为什么handle方法不能访问类属性?
回答by peterm
Properly declare your property
正确申报您的财产
class ImportUsers extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $number; // <-- Here
public function __construct($number)
{
$this->number=$number;
}
public function handle()
{
dd($this->number);
return;
}
}
What happens is after the jobs is being deserialized from the queue you loose dynamically created property.
发生的事情是在从队列中反序列化作业之后,您会丢失动态创建的属性。
Try it:
尝试一下:
$ php artisan tinker >>> Bus::dispatch(new App\Jobs\ImportUsers(7)); 7 >>>