Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nathan_ncm's avatar

Laravel job not passing variable to end view

Evening all!

Trying to get my head around adding queues / jobs into my application...

All working fine however I am struggling to pass variables into my email view, the variable is declared starting in my controller and then need to be passed along the chain - In theory it should work like

Controller -> Job -> Mailer -> View

However the variable isn't reaching my view and I am getting an undefined variable error on $booking.

I am probably misunderstanding how to pass variables between jobs, mailers and views!

CONTROLLER

  public function store(Request $request)
    {
        
	$booking = new Booking;

## Some code here to store request into DB which I have removed as not relevant but you can see booking is declared here and i know this works I am using the variable for other things ##

	// Generate PDF to attach to email with booking details
        $pdf = \PDF::loadView('booking.pdf', compact($booking));

	// Make the job and queue the email 
        SendBookingEmailJob::dispatch($booking, $pdf);


    }

JOB

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public $booking;
    public $pdf;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($booking, $pdf)
    {
        $this->booking = $booking;
        $this->pdf = $pdf;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
      //SEND EMAIL
      Mail::to($this->booking->client->email)
      ->send(new BookingCreatedEmail($this->booking, $this->pdf));
    }
}

MAILER

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class BookingCreatedEmail extends Mailable
{
    use Queueable, SerializesModels;
    public $pdf;
    public $booking;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($pdf, $booking)
    {
        $this->pdf = $pdf;
        $this->booking = $booking;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
      return $this->view('emails.bookingCreate')
      ->with('booking', $this->booking)
      ->subject('Booking Created')
      ->attachData($this->pdf->output(),'booking.pdf');
    }
}

VIEW

<h3>{{$booking->client->contact_first_name}} {{$booking->client->contact_surname}}</h3>
 <pre>

{{$booking->client->address1}}
{{$booking->client->address2}}
{{$booking->client->town()->value('town')}}
{{$booking->client->county()->value('county')}}
{{$booking->client->country()->value('country')}}
{{$booking->client->postcode}}
</pre>

ERROR

Facade\Ignition\Exceptions\ViewException
Undefined variable: booking

Any help would be much appreciated as there only 2 posts on this matter and neither has found a solution yet! Thank you.

0 likes
4 replies
MichalOravec's avatar

You constructor in the BookingCreatedEmail is

public function __construct($pdf, $booking)
{
    $this->pdf = $pdf;
    $this->booking = $booking;
}

But you pass parameters in the opposite way

Mail::to($this->booking->client->email)
      ->send(new BookingCreatedEmail($this->booking, $this->pdf));

So change constructor in the BookingCreatedEmail to

public function __construct($booking, $pdf)
{
    $this->booking = $booking;
    $this->pdf = $pdf;
}
iloveYou's avatar

@MichalOravec Are you kidding? They can be copied in any order, as well as using constructor properties promotion in PHP 8+, this not solves original question.

This looks like some bug in Laravel Queues, just rolled up fresh Laravel instance and made ShouldQueue mailable using official docs, but having this problem too.

Snapey's avatar

@iloveYou you are the one who is kidding

why not start a new topic with your specific question instead of spamming one that is over a year old

1 like
pn0326's avatar

The same kind of issue facing. Any solution guys.

Please or to participate in this conversation.