Skip to content

Lesson 18 of 22 · Request Handling and API Design

Laravel Form Request Shared by Create and Update

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

Two controllers still type-hint ReservationRequest: RequestReservationController, which creates a reservation, and ChangeReservationDatesController, which moves one. The class has served both since the resource controller was split, and it copes by asking which HTTP method it is running under:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use Illuminate\Foundation\Http\FormRequest; final class ReservationRequest extends FormRequest { public function authorize(): bool { return $this->user() !== null; } /** * @return array<string, list<string>> */ public function rules(): array { $creating = $this->isMethod('post'); return [ 'room_id' => [$creating ? 'required' : 'sometimes', 'integer', 'exists:rooms,id'], 'check_in' => [$creating ? 'required' : 'sometimes', 'date_format:Y-m-d', 'after_or_equal:today'], 'check_out' => [$creating ? 'required' : 'sometimes', 'date_format:Y-m-d', 'after:check_in'], 'adults' => [$creating ? 'required' : 'sometimes', 'integer', 'min:1'], 'children' => ['sometimes', 'integer', 'min:0'], 'status' => ['sometimes', 'in:cancelled,checked_in'], ]; } }

Reading a rule means evaluating a ternary in your head for the method you think you are on. Every rule that differs between the two operations is a conditional, and every rule that should differ but does not is a bug that looks like a rule.

Four rules that are wrong for at least one caller

room_id is sometimes on PATCH. A client that sends {"check_in": "...", "check_out": "...", "room_id": 7} to the dates endpoint passes validation. ChangeReservationDatesController reads the two dates and nothing else, so the reservation keeps its old room. The client believes it moved the guest to room 7. The correct rule for the dates endpoint is prohibited: a different room is a different reservation, and sending one should be an error.

check_in and check_out are sometimes on PATCH. A client that sends only check_out passes. The controller reads $request->string('check_in')->toString(), gets an empty string, and the handler's CarbonImmutable::parse('') returns the current moment. The stay silently starts today. Moving a stay needs both dates, so on the dates endpoint both are required. The sometimes was only ever there for the other caller.

adults and children have no business on the dates endpoint at all. They pass, and they are ignored, which is the room_id problem again with a smaller blast radius.

status has not been read by anyone since the old PATCH was deleted. It still validates, so a client that sends status: checked_in on either endpoint gets a 201 or a 204 and no hint that the field did nothing. A rule that nobody reads is a promise the API does not keep.

The authorize() is shared too. Creating a reservation through the web form needs an account linked to a guest, because RequestReservationController reads $request->user()->guest_id. A staff account with no guest record passes authorize(), reaches the controller, the command gets a guest id of 0, and the insert fails on the foreign key. Moving dates has no such requirement.

None of these are visible from a test that sends the payload the frontend sends.

One request per operation

Split the class. Each request lives in the slice it serves, next to the controller that type-hints it.

PHP
<?php declare(strict_types=1); namespace App\Reservations\RequestReservation; use Illuminate\Foundation\Http\FormRequest; final class RequestReservationRequest extends FormRequest { public function authorize(): bool { return $this->user()?->guest_id !== null; } /** * @return array<string, list<string>> */ public function rules(): array { return [ 'room_id' => ['required', 'integer', 'exists:rooms,id'], 'check_in' => ['required', 'date_format:Y-m-d', 'after_or_equal:today'], 'check_out' => ['required', 'date_format:Y-m-d', 'after:check_in'], 'adults' => ['required', 'integer', 'min:1'], 'children' => ['required', 'integer', 'min:0'], ]; } }
PHP
<?php declare(strict_types=1); namespace App\Reservations\ChangeReservationDates; use Illuminate\Foundation\Http\FormRequest; final class ChangeReservationDatesRequest extends FormRequest { public function authorize(): bool { return $this->user() !== null; } /** * @return array<string, list<string>> */ public function rules(): array { return [ 'room_id' => ['prohibited'], 'adults' => ['prohibited'], 'children' => ['prohibited'], 'check_in' => ['required', 'date_format:Y-m-d', 'after_or_equal:today'], 'check_out' => ['required', 'date_format:Y-m-d', 'after:check_in'], ]; } }

No ternaries. status is gone from both, because neither operation reads it. children became required on create, because the old sometimes was hiding a field the handler always writes; a request without it produced a reservation with zero children by way of integer() returning its default, and nobody had decided that. room_id, adults and children are prohibited on the dates endpoint, so the client that thought it moved the guest to room 7, or changed the party size, now gets a 422 that says so.

The two controllers type-hint their own request. The create side is RequestReservationController with the request type changed:

PHP
<?php declare(strict_types=1); namespace App\Reservations\RequestReservation; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Http\JsonResponse; final readonly class RequestReservationController { public function __construct( private Dispatcher $bus, ) { } public function __invoke(RequestReservationRequest $request): JsonResponse { $reservation = $this->bus->dispatch(new RequestReservationCommand( guestId: (int) $request->user()->guest_id, roomId: $request->integer('room_id'), checkIn: $request->string('check_in')->toString(), checkOut: $request->string('check_out')->toString(), adults: $request->integer('adults'), children: $request->integer('children'), )); return (new RequestReservationResource($reservation)) ->response() ->setStatusCode(201); } }
PHP
<?php declare(strict_types=1); namespace App\Reservations\ChangeReservationDates; use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Http\JsonResponse; final readonly class ChangeReservationDatesController { public function __construct( private Dispatcher $bus, ) { } public function __invoke(ChangeReservationDatesRequest $request, int $id): JsonResponse { $this->bus->dispatch(new ChangeReservationDatesCommand( reservationId: $id, checkIn: $request->string('check_in')->toString(), checkOut: $request->string('check_out')->toString(), )); return new JsonResponse(null, 204); } }

app/Reservations/ReservationRequest.php is deleted. Nothing type-hints it.

Why the shared class felt right

Create and update touch the same table, so sharing their validation looks like removing duplication, but only two of the five fields have the same rule on both endpoints. What the two operations share is a model. What they do not share is a contract with the client, and the request class is the contract.

The same split applies to authorize(). A shared request can only ask a question both callers can answer, which is always the weaker question. Two requests ask two questions, and the create side now asks the one it needed all along: is this user a guest.

What the import does not get

The channel import has no Form Request. It builds RequestReservationCommand from a payload and dispatches it, and none of the rules above run for it. That is correct for exists:rooms,id and after_or_equal:today, which are rules about what the web form may submit. It is a problem for after:check_in and min:1 on adults, which are rules about what a reservation is. A stay that ends before it starts is wrong whoever sends it, and right now the only code that knows that is a rule the import never sees.

Rules that describe the input belong in the request. Rules that describe the thing belong on the thing. The next chapter moves the second kind into objects that both callers construct, so the import cannot build a stay with the dates reversed any more than the form can.

The one thing to remember

Create and update share a model, not a contract. One request per operation, in the operation's own folder, and the rules say what they mean without a method check.

Course Content