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.