我正在使用Lumen
,想知道如何将data
传递给我的job
课程。
我有job
类,如下所示;
<?php
namespace App\Jobs;
use App;
use Illuminate\Contracts\Mail\Mailer;
class TestEmailJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Mailer $mailer)
{
//
$data; // I want to pass this from my function
$mailer->queue('emails.emailtemplate', $data , function ($message) {
$message->from('support@xxx.com', 'Laravel');
$message->to('xxx@gmail.com')->cc('xxx@yahoo.co.uk');
});
}
}
然后我有一个功能来推动队列中的作业;
public function emailTest () {
$data = ['user' => $user];
Queue::push(new TestEmailJob);
}
如何通过$data
并接收并在job
课程中使用它?
答案 0 :(得分:2)
您可以通过传递给构造函数
来实例化变量class TestEmailJob extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue;
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function handle()
{
// $this->data;
}
}
现在,您可以通过Controller调用它:
$this->dispatch(new TestEmailJob($data));