Skip to content

Lesson 13 of 22 · Architecture and Code Organization

Laravel Folder Structure Is Not an Architecture

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

Here is every file that takes part in "request a reservation" after the last lesson.

TEXT
app/Http/Controllers/Reservations/RequestReservationController.php app/Http/Requests/ReservationRequest.php app/Http/Resources/ReservationResource.php app/Services/RequestReservation.php app/Models/Reservation.php app/Models/Guest.php app/Models/Room.php app/Models/RoomType.php app/Pricing/NightlyRatePricing.php app/Mail/ReservationConfirmation.php routes/api.php

Seven folders under app, plus the routes file. Apart from app/Pricing and the Reservations folder inside the controllers, they are named after the kind of file they hold. app/Models holds the reservation model next to the room, the guest, the payment, the user, and everything else the application has a table for. app/Services is where the use case class went because it was neither a controller nor a model, and that folder will hold every other class that fits neither description as well.

This is the layout make:controller, make:model, and make:request produce, and it is the layout of the default skeleton.

MVC is a UI pattern

The skeleton is MVC. Models in app/Models, views in resources/views or, for a JSON API, the resource classes, controllers in app/Http/Controllers. MVC separates a UI from its data and from the input handling between them. It has no place for a use case. Where does "a stay of seven nights gets ten percent off" live? How do the web form and the channel import share "request a reservation"? Where does the rule that a cancelled reservation cannot be checked in go? When MVC is the whole plan, those questions get answered by default, and the default answer is always "the controller", because that is the only place MVC provides. When the controllers fill up, the answer becomes "the model", and when the models fill up, someone invents app/Services.

A module folder

A module is a folder, and a namespace, that holds everything about one capability the business has, named in the business's own words. This application has reservations, guests, rooms, pricing, and reporting. So app/Reservations, app/Guests, app/Rooms, app/Pricing, and the app/Reports folder that already exists. Inside a module, a folder per action holds all the files that action needs, and a folder per model holds the model with its enums and its factory.

TEXT
app/ Reservations/ ReservationsServiceProvider.php reservations-routes.php ReservationRequest.php ReservationConfirmation.php Reservation/ Reservation.php ReservationStatus.php ReservationFactory.php Payment/ Payment.php PaymentFactory.php RequestReservation/ RequestReservation.php RequestReservationController.php RequestReservationResource.php ShowReservation/ ShowReservationController.php ShowReservationResource.php ListReservations/ ListReservationsController.php ListReservationsRequest.php ListReservationsResource.php ChangeReservation/ ChangeReservationController.php CancelReservation/ CancelReservationController.php ImportChannelReservations/ ImportChannelReservationsController.php Guests/ Guest/ Guest.php GuestFactory.php Rooms/ Room/ Room.php RoomFactory.php RoomType/ RoomType.php RoomTypeFactory.php Pricing/ NightlyRatePricing.php Reports/ OccupancyReport.php OccupancyRow.php

User stays in app/Models. The app directory is autoloaded by PSR-4, so App\Reservations\Reservation\Reservation resolves as long as the folder path matches the namespace. What Laravel needs to be told is where the routes are, and the module tells it through its own service provider.

The shared ReservationResource from app/Http/Resources splits the same way, one resource per action that returns a body, so RequestReservationResource sits next to its controller. The shared Form Request moves to the module root as App\Reservations\ReservationRequest and stays shared for now, because two actions still use it, and the list gets a request of its own because it reads different fields. A later lesson shows the list request.

RequestReservation moves into its action folder, and its imports point at the module. app/Services is deleted. The status becomes a ReservationStatus case, which the model section below introduces. RequestReservationTest and NightlyRatePricingTest need their use lines changed to the new namespaces, and the status assertion in RequestReservationTest expects ReservationStatus::CONFIRMED instead of 'confirmed'.

PHP
<?php declare(strict_types=1); namespace App\Reservations\RequestReservation; use App\Guests\Guest\Guest; use App\Pricing\NightlyRatePricing; use App\Reservations\Reservation\Reservation; use App\Reservations\Reservation\ReservationStatus; use App\Reservations\ReservationConfirmation; use App\Rooms\Room\Room; use Carbon\CarbonImmutable; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Support\Str; final readonly class RequestReservation { public function __construct( private NightlyRatePricing $pricing, private Mailer $mailer, ) { } /** * @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' => ReservationStatus::CONFIRMED, 'confirmed_at' => CarbonImmutable::now(), 'confirmation_code' => Str::upper(Str::random(8)), 'total_cents' => $this->pricing->totalCents($room->roomType, $checkIn, $checkOut), ]); $this->mailer->to($guest->email)->send(new ReservationConfirmation($reservation)); return $reservation; } }

The mailable moves to the module root. It is like the one make:mail generates, with the model as a public property so the template can read the guest, room, and dates off it:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Reservations\Reservation\Reservation; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; final class ReservationConfirmation extends Mailable { 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 controller moves into the action folder and calls the use case class the way the old one did.

PHP
<?php declare(strict_types=1); namespace App\Reservations\RequestReservation; use App\Reservations\ReservationRequest; use Illuminate\Http\JsonResponse; final readonly class RequestReservationController { public function __construct( private RequestReservation $requestReservation, ) { } public function __invoke(ReservationRequest $request): JsonResponse { $reservation = $this->requestReservation->handle( guestId: (int) $request->user()->guest_id, attributes: $request->validated(), ); return (new RequestReservationResource($reservation))->response()->setStatusCode(201); } }
PHP
<?php declare(strict_types=1); namespace App\Reservations; use Illuminate\Support\ServiceProvider; final class ReservationsServiceProvider extends ServiceProvider { public function boot(): void { $this->loadRoutesFrom(__DIR__.'/reservations-routes.php'); } }
PHP
<?php declare(strict_types=1); use App\Reservations\CancelReservation\CancelReservationController; use App\Reservations\ChangeReservation\ChangeReservationController; use App\Reservations\ImportChannelReservations\ImportChannelReservationsController; use App\Reservations\ListReservations\ListReservationsController; use App\Reservations\RequestReservation\RequestReservationController; use App\Reservations\ShowReservation\ShowReservationController; use Illuminate\Support\Facades\Route; Route::prefix('api')->middleware(['api', 'auth:sanctum'])->group(function (): void { Route::get('/reservations', ListReservationsController::class)->name('reservations.list'); Route::post('/reservations', RequestReservationController::class)->name('reservations.request'); Route::get('/reservations/{id}', ShowReservationController::class)->name('reservations.show'); Route::patch('/reservations/{id}', ChangeReservationController::class)->name('reservations.change'); Route::post('/reservations/{id}/cancel', CancelReservationController::class)->name('reservations.cancel'); Route::post('/reservations/import', ImportChannelReservationsController::class)->name('reservations.import'); });

The import controller is renamed ImportChannelReservationsController and gets its route here. Delete the reservation routes and the import route from routes/api.php, or they register twice. routes/api.php was given the api prefix and the api middleware group by the framework when it registered that file. A file loaded with loadRoutesFrom() gets neither, so the module's route file declares both and POST /api/reservations still reaches the same controller.

The provider is registered once, in bootstrap/providers.php.

PHP
<?php declare(strict_types=1); return [ App\Providers\AppServiceProvider::class, App\Reservations\ReservationsServiceProvider::class, ];

The import controller moves into the module.

PHP
<?php declare(strict_types=1); namespace App\Reservations\ImportChannelReservations; use App\Guests\Guest\Guest; use App\Reservations\RequestReservation\RequestReservation; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; final readonly class ImportChannelReservationsController { public function __construct( private 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); } }

The model moves, and loses its fillable list

Reservation lives in the module now, in a folder with its status enum and its factory. The status stops being a string that files compare against by hand.

PHP
<?php declare(strict_types=1); namespace App\Reservations\Reservation; enum ReservationStatus: string { case CONFIRMED = 'confirmed'; case CHECKED_IN = 'checked_in'; case CHECKED_OUT = 'checked_out'; case CANCELLED = 'cancelled'; }
PHP
<?php declare(strict_types=1); namespace App\Reservations\Reservation; use App\Guests\Guest\Guest; use App\Reservations\Payment\Payment; use App\Rooms\Room\Room; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; class Reservation extends Model { /** @use HasFactory<ReservationFactory> */ use HasFactory; protected function casts(): array { return [ 'status' => ReservationStatus::class, 'check_in' => 'immutable_date', 'check_out' => 'immutable_date', 'confirmed_at' => 'immutable_datetime', ]; } protected static function newFactory(): ReservationFactory { return new ReservationFactory(); } public function guest(): BelongsTo { return $this->belongsTo(Guest::class); } public function room(): BelongsTo { return $this->belongsTo(Room::class); } public function payments(): HasMany { return $this->hasMany(Payment::class); } public function outstandingCents(): int { return $this->total_cents - (int) $this->paid_cents; } }

Moving the model out of App\Models breaks the default factory lookup, which maps App\Models\Reservation to Database\Factories\ReservationFactory, so the model names its factory in newFactory(), and ReservationFactory names its model with protected $model = Reservation::class;, because the reverse guess fails the same way.

$fillable is gone, and the same happens to every model as it moves. The list existed to protect create($request->all()) from writing columns the client was not supposed to set. No code in the module passes $request->all() to create() any more. What the list was costing was a second copy of the column list in every model. One line in the application provider replaces it, next to the existing shouldBeStrict() call.

PHP
<?php declare(strict_types=1); namespace App\Providers; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\ServiceProvider; final class AppServiceProvider extends ServiceProvider { public function boot(): void { Model::unguard(); Model::shouldBeStrict(); } }

unguard() turns mass-assignment protection off for every model, so the only guard left is the code that builds the array passed to create(). shouldBeStrict(), already on, turns on two checks that matter here: a lazy relation load from a collection throws instead of running N queries, the problem the Queries and Data Access chapter chased through the query log; and reading an attribute that was never loaded throws instead of returning null.

The one thing to remember

Name folders after what the business does, and put everything one action needs in one place.

Course Content