Don't Save the Row From Its Own Observer
An observer's created hook runs right after the INSERT, so every save() inside it is another write. Submit one reservation and the query log shows the INSERT followed by an UPDATE per save() in the hook, each committed on its own because nothing wraps them in a transaction. The last write is the one that does the lookup, and when it throws, the row is already confirmed in the database.
Don't do this:
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\Reservation;
use Illuminate\Support\Str;
final class ReservationObserver
{
public function created(Reservation $reservation): void
{
$reservation->confirmation_code = Str::upper(Str::random(8));
$reservation->status = 'confirmed';
$reservation->save();
$nights = (int) $reservation->check_in->diffInDays($reservation->check_out);
$reservation->total_cents = $nights * $reservation->room->roomType->nightly_rate_cents;
$reservation->save();
}
}Do this instead:
<?php
declare(strict_types=1);
namespace App\Reservations;
use App\Models\Guest;
use App\Models\Reservation;
use App\Models\Room;
use Carbon\CarbonImmutable;
use Illuminate\Support\Str;
final readonly class ConfirmReservation
{
public function handle(
Guest $guest,
Room $room,
CarbonImmutable $checkIn,
CarbonImmutable $checkOut,
): Reservation {
$nights = (int) $checkIn->diffInDays($checkOut);
return Reservation::query()->create([
'guest_id' => $guest->id,
'room_id' => $room->id,
'check_in' => $checkIn,
'check_out' => $checkOut,
'status' => 'confirmed',
'confirmation_code' => Str::upper(Str::random(8)),
'total_cents' => $nights * $room->roomType->nightly_rate_cents,
]);
}
}The first version commits the code and status, then fails on a room whose type was archived: a confirmed reservation with no total. The second decides every column first and writes once, so a failed rate lookup throws before anything is stored. The channel manager import calls the same class and gets the same single INSERT.
In Laravel 13, Eloquent has not synced the model's original attributes when created fires, so the extra save() also rewrites columns it never changed. A listener mapped through $dispatchesEvents behaves the same way. Observers and listeners are for side effects such as mail and counters, not for finishing the row.