Skip to content

Lesson 20 of 22 · Primitives and Value Objects

Laravel Queued Mailables That Carry Eloquent Models

Intermediate•
lesson•
November 30, 2026•
~6 min read

The row from the channel and the result from the handler are typed now. Two Eloquent models still travel further than they should. The confirmation mailable takes the whole Reservation and rides the queue with it. The pricing class takes a RoomType to read one integer off it. Both are the Laravel default: you have a model in your hand, so you pass the model.

The model in the queue

Here is the SendReservationConfirmation listener. The event carries ids, the listener loads the reservation, and hands the model to a queued mailable:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Reservations\RequestReservation\ReservationConfirmed; use App\Reservations\Reservation\Reservation; use Illuminate\Contracts\Mail\Mailer; final readonly class SendReservationConfirmation { public function __construct( private Mailer $mailer, ) { } public function handle(ReservationConfirmed $event): void { $reservation = Reservation::query() ->with(['guest', 'room']) ->findOrFail($event->reservationId); $this->mailer ->to($reservation->guest->email) ->send(new ReservationConfirmation($reservation)); } }
PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Reservations\Reservation\Reservation; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; final class ReservationConfirmation extends Mailable implements ShouldQueue { use Queueable; use SerializesModels; public function __construct(public Reservation $reservation) { } public function envelope(): Envelope { return new Envelope( subject: 'Your reservation '.$this->reservation->confirmation_code, ); } public function content(): Content { return new Content(markdown: 'mail.reservation-confirmation'); } }

The template reads $reservation->guest->name, $reservation->room->number, the dates, and $reservation->total_cents. This is the shape php artisan make:mail generates, and it works, so here is what SerializesModels does with that public property.

When send() runs on a mailable that implements ShouldQueue, Laravel pushes a job onto the queue that contains the mailable. Serialising the mailable serialises its properties. For any property holding an Eloquent model, SerializesModels does not store the row. It stores a ModelIdentifier: the model's class, its id, the names of the relations that were loaded, and the connection name. When a worker picks the job up, the trait runs a fresh query for that id (firstOrFail() on the write connection) and calls loadMissing() for the relation names it stored.

Two things follow from that, and neither shows up while you are writing the code.

The mail renders the row as it is when the worker runs, not as it was when the guest requested the reservation. If the queue is backed up and the guest changes the dates in the meantime, the confirmation shows the new dates and the new total.

If the row is gone when the worker runs, firstOrFail() throws ModelNotFoundException and the job fails. A staff member deleting a duplicate channel import a minute after it landed produces a failed job about an email for a reservation that no longer exists.

The fix is to hand the mailable the values it renders, captured at the moment the listener runs:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use Carbon\CarbonImmutable; final readonly class ReservationConfirmationDetails { public function __construct( public string $guestName, public string $guestEmail, public string $confirmationCode, public string $roomNumber, public CarbonImmutable $checkIn, public CarbonImmutable $checkOut, public int $totalCents, ) {} }
PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Reservations\RequestReservation\ReservationConfirmed; use App\Reservations\Reservation\Reservation; use Illuminate\Contracts\Mail\Mailer; final readonly class SendReservationConfirmation { public function __construct( private Mailer $mailer, ) { } public function handle(ReservationConfirmed $event): void { $reservation = Reservation::query() ->with(['guest', 'room']) ->findOrFail($event->reservationId); $details = new ReservationConfirmationDetails( guestName: $reservation->guest->name, guestEmail: $reservation->guest->email, confirmationCode: $reservation->confirmation_code, roomNumber: $reservation->room->number, checkIn: $reservation->check_in, checkOut: $reservation->check_out, totalCents: $reservation->total_cents, ); $this->mailer ->to($details->guestEmail) ->send(new ReservationConfirmation($details)); } }
PHP
<?php declare(strict_types=1); namespace App\Reservations; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; final class ReservationConfirmation extends Mailable implements ShouldQueue { use Queueable; public function __construct(public ReservationConfirmationDetails $details) { } public function envelope(): Envelope { return new Envelope( subject: 'Your reservation '.$this->details->confirmationCode, ); } public function content(): Content { return new Content(markdown: 'mail.reservation-confirmation'); } }

SerializesModels is gone from the mailable because there is no model to serialise. The details object is strings, an integer, and two immutable dates, and PHP serialises those as they are. The worker renders what the listener saw. A deleted reservation does not fail the job. The template reads $details->guestName instead of walking a relation.

The listener still loads the model. That is the right place for it: the listener is the last step that runs before the queue, it has the ids from the event, and it turns rows into values before anything crosses into the queue.

The model in the pricing class

NightlyRatePricing took the price calculation out of the trait and kept the trait's habit of reading the rate off the model:

PHP
<?php declare(strict_types=1); namespace App\Pricing; use App\Rooms\RoomType\RoomType; use Carbon\CarbonImmutable; final readonly class NightlyRatePricing { private const WEEKLY_DISCOUNT_PERCENT = 10; private const WEEKLY_DISCOUNT_MINIMUM_NIGHTS = 7; public function totalCents(RoomType $roomType, CarbonImmutable $checkIn, CarbonImmutable $checkOut): int { $nights = (int) $checkIn->diffInDays($checkOut); $total = $nights * $roomType->nightly_rate_cents; if ($nights >= self::WEEKLY_DISCOUNT_MINIMUM_NIGHTS) { $total = (int) round($total * (100 - self::WEEKLY_DISCOUNT_PERCENT) / 100); } return $total; } }

The class uses one column. Its signature demands the whole model, so anyone calling it needs a RoomType, and the class itself is tied to the name nightly_rate_cents. Rename that column and the pricing class changes although no pricing rule did. Test the seven-night discount and you are building a RoomType to carry one integer into a multiplication.

The handler already has the model loaded. It can pass the integer:

PHP
<?php declare(strict_types=1); namespace App\Pricing; use Carbon\CarbonImmutable; final readonly class NightlyRatePricing { private const WEEKLY_DISCOUNT_PERCENT = 10; private const WEEKLY_DISCOUNT_MINIMUM_NIGHTS = 7; public function totalCents(int $nightlyRateCents, CarbonImmutable $checkIn, CarbonImmutable $checkOut): int { $nights = (int) $checkIn->diffInDays($checkOut); $total = $nights * $nightlyRateCents; if ($nights >= self::WEEKLY_DISCOUNT_MINIMUM_NIGHTS) { $total = (int) round($total * (100 - self::WEEKLY_DISCOUNT_PERCENT) / 100); } return $total; } }

The call in RequestReservationCommandHandler::handle() becomes $this->pricing->totalCents($room->roomType->nightly_rate_cents, $checkIn, $checkOut), and the call in ChangeReservationDatesCommandHandler changes the same way, and so does the quote page's. Nothing else in either handler changes.

The NightlyRatePricingTest already ran without a database. Now it passes the integer:

PHP
<?php declare(strict_types=1); namespace Tests\Unit\Pricing; use App\Pricing\NightlyRatePricing; use Carbon\CarbonImmutable; use PHPUnit\Framework\TestCase; final class NightlyRatePricingTest extends TestCase { public function test_a_six_night_stay_pays_the_full_rate(): void { $total = (new NightlyRatePricing())->totalCents( nightlyRateCents: 12000, checkIn: CarbonImmutable::parse('2026-10-01'), checkOut: CarbonImmutable::parse('2026-10-07'), ); $this->assertSame(72000, $total); } public function test_a_seven_night_stay_gets_ten_percent_off(): void { $total = (new NightlyRatePricing())->totalCents( nightlyRateCents: 12000, checkIn: CarbonImmutable::parse('2026-10-01'), checkOut: CarbonImmutable::parse('2026-10-08'), ); $this->assertSame(75600, $total); } }

PHPUnit\Framework\TestCase, not Tests\TestCase: no application boots and no database migrates. The pricing class no longer knows that room types exist.

Where the model belongs

The handler loads the room, reads the rate, creates the reservation, and dispatches an event with ids. The listener loads the reservation and turns it into details. Everything past those two points, the mailable, the pricing rule, the event, the result, holds values. That is the boundary: a model is loaded and used inside the class that queried for it, and what leaves that class is data.

The one thing to remember

An Eloquent model is a row with a connection attached. Keep it inside the handler or listener that loaded it, and hand everything further out (the mailable, the pricing rule, the queue) the values it needs rather than the row they came from.

Course Content