将数据传递给Mail Job / Queue - Lumen / Laravel

时间:2016-02-10 16:10:03

标签: laravel laravel-5 lumen

我正在使用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课程中使用它?

1 个答案:

答案 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));

http://laravel.com/docs/5.1/queues#writing-job-classes