Skip to content

Lesson 9 of 22 · Architecture and Code Organization

Laravel Auth::user() Inside the Use Case

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

RequestReservation now has two callers, and the first line of handle(), the one that reads Auth::user()->guest, is wrong for one of them.

In the browser this line is right. The session middleware started the session, the default guard loads the user from it, and that user's guest is the one making the reservation. In the import it is wrong. Auth::user() returns whoever authenticated the channel manager's push: the staff member whose API token is configured on the channel side. Every imported reservation gets attached to that staff member's guest record, or ->guest returns null and $guest->id throws on the first row. Neither outcome has anything to do with the row being imported, which names its own guest.

The dependency you cannot see

Read the signature of handle(): it takes an array of attributes and returns a Reservation. Read the body and the real contract is different. The method also needs an authenticated user on the default guard, and it needs that user to have a guest. Nothing in the type hints says so. Nothing at the call site says so. You find out when you call it from somewhere that has no guard, or the wrong one.

The places with no guard are exactly the places a second caller lives. A queued job runs on a worker process with no session, so Auth::user() returns null. An Artisan command has the same problem. A test has to call actingAs() before it can request a reservation, even though the thing under test has nothing to do with logging in.

The mistake is where Auth::user() gets called. The controller is the one place that knows there is a request, a session, and a guard, so the controller resolves the user and hands the use case class the piece it needs.

Pass the guest in

The use case class writes a guest_id, so that is what it takes. Not the User, not the Guest model, the id.

PHP
<?php declare(strict_types=1); namespace App\Services; use App\Mail\ReservationConfirmation; use App\Models\Guest; use App\Models\Reservation; use App\Models\Room; use Carbon\CarbonImmutable; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Str; final class RequestReservation { /** * @param array<string, mixed> $attributes */ public function handle(int $guestId, array $attributes): Reservation { $guest = Guest::query()->findOrFail($guestId); $room = Room::query()->with('roomType')->findOrFail($attributes['room_id']); $checkIn = CarbonImmutable::parse($attributes['check_in']); $checkOut = CarbonImmutable::parse($attributes['check_out']); $reservation = Reservation::query()->create([ 'guest_id' => $guest->id, 'room_id' => $room->id, 'check_in' => $checkIn, 'check_out' => $checkOut, 'adults' => $attributes['adults'], 'children' => $attributes['children'], 'status' => 'confirmed', 'confirmed_at' => CarbonImmutable::now(), 'confirmation_code' => Str::upper(Str::random(8)), 'total_cents' => app(QuoteService::class)->totalCentsFor($room, $checkIn, $checkOut), ]); Mail::to($guest->email)->send(new ReservationConfirmation($reservation)); return $reservation; } }

The Auth import is gone. The class no longer cares who is logged in, or whether anyone is.

The web controller reads the guest off the authenticated user. Every user who can reach the reservation form has a guest record, because registration creates both rows, so the guest_id column on users is what gets passed.

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Services\RequestReservation; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; class ReservationController extends Controller { public function store(Request $request, RequestReservation $requestReservation): RedirectResponse { $attributes = $request->validate([ 'room_id' => 'required|exists:rooms,id', 'check_in' => 'required|date|after_or_equal:today', 'check_out' => 'required|date|after:check_in', 'adults' => 'required|integer|min:1', 'children' => 'required|integer|min:0', ]); $reservation = $requestReservation->handle( guestId: (int) $request->user()->guest_id, attributes: $attributes, ); return redirect()->route('reservations.show', $reservation); } }

The import has no logged-in guest and never will. What it has is the guest's details in the channel manager's payload, so it finds or creates the guest from those and passes the id.

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Models\Guest; use App\Services\RequestReservation; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class ChannelImportController extends Controller { public function __construct( private readonly RequestReservation $requestReservation, ) { } public function __invoke(Request $request): JsonResponse { $rows = $request->input('reservations', []); foreach ($rows as $row) { $guest = Guest::query()->firstOrCreate( ['email' => $row['guest']['email']], ['name' => $row['guest']['name'], 'phone' => $row['guest']['phone'] ?? null], ); $this->requestReservation->handle( guestId: $guest->id, attributes: [ 'room_id' => $row['room_id'], 'check_in' => $row['arrival'], 'check_out' => $row['departure'], 'adults' => $row['adults'], 'children' => $row['children'], ], ); } return new JsonResponse(['imported' => count($rows)], 200); } }

Each caller answers the question "which guest?" in the only way it can. The browser answers from the session. The import answers from the payload. The use case class asks the question in its signature and answers it for nobody.

A test now reads the same way as the import. It creates a guest with a factory and calls handle(guestId: $guest->id, attributes: [...]), with no actingAs() anywhere. The next lesson writes that test, once the class stops reaching into the container for the rest of what it needs.

The one thing to remember

The authenticated user exists only inside a request, so only the controller reads it. A use case class takes the id it is going to write as an argument, and then any caller with an id can run it: a browser, an import, a job, a test.

Course Content