作业完成后,发送电子邮件并链接到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