douting0585 2019-08-19 03:25
浏览 99

作业完成后,发送电子邮件并链接到Auth用户

I'm using laravel/dompdf to generate a 30+ page PDF and using a Laravel Job and php artisan queue:work to complete the task so it will run in the background.

I want the "Job" to email the Auth user that selected the job to run. It should email once it has completed, with a link to the PDF.

Example:

  1. John is logged in and presses the button to start the PDF job.
  2. The PDF job runs in the background
  3. The PDF job finishes
  4. Once finished, the PDF job script emails John the link to the PDF

This is how my code is laid out:

  1. View to start job
  2. Hits the controller
  3. Dispatch to the Job
  4. Passes info to Mail
  5. Sends mail

The main thing I'm looking for is help getting the Auth users email and first_name passed to the email I'm trying to send once the job is complete.


1. Start Job

<a href="{{ route('create-pdf') }}">Create PDF</a>

2. Controller

use Auth;
use App\User;
use PDF;
use App\Jobs\ProcessPdfHaiti;
...etc
        public function createPdf(){

            $haitiKids = Kid::
            whereRaw('sponsors_received < sponsors_needed')
            ->where('current_country', 'Haiti')
            ->orderBy('sponsors_received', 'ASC')
            ->get();

       ProcessPdfHaiti::dispatch($haitiKids);

       return back()->with('info','This will take a minute. You\'ll receive an email when it\'s completed.');
}

3. Job (ProcessPdfHaiti)

namespace App\Jobs;
use Auth;
use App\User;
...etc

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

    public $haitiKids;

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

    public function handle()
    {
        $haitiKids = Kid::
        whereRaw('sponsors_received < sponsors_needed')
        ->where('current_country', 'Haiti')
        ->orderBy('sponsors_received', 'ASC')
        ->get();

        $pdf =  PDF::loadView('admin.cdp.haiti-kid-pdf-all', compact('haitiKids'))->setPaper('letter', 'landscape');

        $pdf->save(storage_path('app/public') . '/images/cdp/pdf/haiti-kids'.'-'.date("mdyhis").'.pdf');

// ####THIS IS WHERE I GET THE ERROR! ####
        $pdfUserName=  Auth::user()->first_name;
        $pdfUserEmail =  Auth::user()->email;

        Mail::to($pdfUserEmail)
        ->send(new PdfFinished(
            $pdfUserName = $pdfUser->first_name,
            $pdfpath = storage_path('app/public') . '/images/cdp/pdf/haiti-kids'.'-'.date("mdyhis").'.pdf'
            ));
    }
}

This is the above error I'm getting Trying to get property 'first_name' of non-object

4. Passes info to Mail

namespace App\Mail;
use ...etc

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');
    }
}

5. Send email with users First Name and Link

@component('mail::message')
### Hello {{ ucwords(strtolower($pdfUserName)) }},<br>
The PDF has processed successfully and is ready to view.<br>
You can copy and paste this link into the browser {{ $pdfpath }} to download, or select the button below.

{{-- WAS TRYING TO USE A BUTTON, BUT COULDN'T FIGURE OUT HOW TO ADD THE LINK TO THE URL
@component('mail::button', ['url' => {{ $pdfpath }}, 'color' => 'green'])
Download PDF
@endcomponent --}}

...etc
@endcomponent

6. I make sure I have php artisan queue:work running on the server

This is my first time creating a Jobs and Queue in Laravel so any help would be appreciated.


Update (Based on porloscerros solution)

        $pdfUser = Auth::user();
//GET ERROR HERE
        $pdfUserEmail =  $pdfUser->email;
        $pdfUserName =  $pdfUser->first_name;

        Mail::to($pdfUserEmail)
        ->send(new PdfFinished(
            $pdfUserName = $pdfUserName,
            $pdfpath = storage_path('app/public') . '/images/cdp/pdf/haiti-kids'.'-'.date("mdyhis").'.pdf'
            ));

ERROR: Trying to get property 'email' of non-object

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多