将Auth用户数据传递到队列作业类

时间:2019-08-21 15:52:42

标签: php laravel jobs

我创建了一个在后台处理PDF的作业。作业完成后,我想发送一封电子邮件给Auth用户,并提供下载新生成的PDF的链接。

这是我目前正在做的事情。

控制器:

        public function haitiKidPdfAll(){
            $pdfUser = User::find(Auth::user()->id);
            $haitiKids = Kid::
            whereRaw('sponsors_received < sponsors_needed')
            ->where('current_country', 'Haiti')
            ->orderBy('sponsors_received', 'ASC')
            ->get();

            ProcessPdfHaiti::dispatch($haitiKids,$pdfUser);
            return back()->with('info','This will take a couple minutes. I\'ll email you when it\'s completed.');

ProcessPdfHaiti作业:

  

在此处获取错误:未定义的变量:pdfUser {“ exception”:“ [对象]   (ErrorException(code:0):未定义的变量:pdfUser第53行。   在下面的代码中是$pdfUserEmail = $pdfUser->email;

class ProcessPdfHaiti implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $haitiKids;
    public $pdfUser;

    public function __construct($haitiKids,$pdfUser)
    {
        $this->haitiKids = $haitiKids;
        $this->pdfUser = $pdfUser;
    }

    public function handle()
    {

        ...PDF Query Stuff

        $pdfUserEmail =  $pdfUser->email;
        $pdfUserName =  $pdfUser->first_name;

        //I WANT TO EMAIL THE AUTH USER HERE!!! Then Pass the Auth Users Name to the email.
        Mail::to($pdfUserEmail)
        ->send(new PdfFinished(
            $pdfUserName = $pdfUserName,
            ));
    }
}

可发送邮件:

class PdfFinished extends Mailable
{
    use Queueable, SerializesModels;

    public $pdfUserName;
    public $pdfpath;

    public function __construct($pdfUserName,$pdfpath)
    {
        $this->pdfUserName =$pdfUserName;
        $this->pdfpath =$pdfpath;
    }

    public function build()
    {
        return $this->subject('PDF Has Completed')->markdown('emails.staff.pdfcompleted');
    }
}

向授权用户发送电子邮件:

@component('mail::message')
### Hello {{ $pdfUserName }},<br>
..etc
@endcomponent

在此待了几天。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您需要使用$this关键字来访问它们,就像您在工作的constructor中对其进行了初始化一样。

$pdfUserEmail = $this->pdfUser->email;
$pdfUserName = $this->pdfUser->first_name;

此外,Auth::user()返回一个App \ User的实例,因此您无需执行User::find,因为您已经拥有一个User模型。因此,您实际上可以这样称呼您的工作:

ProcessPdfHaiti::dispatch($haitiKids, Auth::user());

希望有帮助:)

相关问题