将身份验证用户数据传递到队列作业类

cwd*_*cwd 1 php jobs laravel

我创建了一个在后台处理 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.');
Run Code Online (Sandbox Code Playgroud)

流程Pdf海地工作:

此处出现错误:未定义的变量:pdfUser {“异常”:“[对象](ErrorException(代码:0):未定义的变量:第53行的pdfUser。在$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,
            ));
    }
}
Run Code Online (Sandbox Code Playgroud)

可邮寄:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

已经这样好几天了。任何帮助,将不胜感激。

par*_*ing 5

您需要使用$this关键字访问它们,因为您已经在作业的构造函数中初始化了它们。

$pdfUserEmail = $this->pdfUser->email;
$pdfUserName = $this->pdfUser->first_name;
Run Code Online (Sandbox Code Playgroud)

另外,Auth::user()返回 App\User 的实例,因此您不需要执行任何操作,User::find因为您已经有了 User 模型。所以你实际上可以这样称呼你的工作:

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

希望有帮助:)