Reviewing Ergodnc - What a Clean Laravel API Still Gets Wrong
Ergodnc (Ergonomic Desk & Coffee, per its README) is the office-rental API Mohamed Said built live on the official Laravel YouTube channel. The repository is themsaid/ergodnc, and this review reads it at commit 42907c0 from 26 November 2021. It runs Laravel 8 (^8.54 in composer.json, 8.63.0 in the lock file) with Sanctum, has nine controllers, five models, four feature test files with 39 tests, and no unit tests. Hosts publish offices, guests reserve them by the day, admins approve new offices.
It is a good codebase by the standards I see applied in most Laravel codebases I review. Controllers are short, output goes through API resources, six of the seven write endpoints have tests, models are unguarded on purpose, and the morph map is enforced. The reason to review it is exactly that it is clean by those standards and still fails the rules that matter once a codebase has to survive years of change.
The rulebook
The audit uses a fixed rulebook, which I call the canon, that I apply to every Laravel codebase I want to keep for the long term. It ignores the project's own conventions. In short: controllers and handlers coordinate, and the model that owns the data owns the rule. Each capability is one vertical slice folder (command, handler, controller, request, resource, event, listener). Each business area is a module that reaches other modules only through interfaces it defines, through events, or through read-only queries by id. Authorization comes from the authenticated user, and the ownership check lives inside the model lookup. The rebuilt code targets Laravel 13.
The five reviews
Each review takes one flow, shows the current code, lists what is wrong with it, and rebuilds it as a slice.
POST /reservations: Refactoring Ergodnc - Moving Business Logic Out of a Laravel Controller. The price moves into a value object, the guards move onto the model, and the notifications move into a listener.DELETE /reservations/{id}: Refactoring Ergodnc - Putting the Ownership Check Inside the Lookup. One 422 for three different failures becomes a 403 and two 409s, and the endpoint returns 204.POST /officesandPUT /offices/{id}: Refactoring Ergodnc - From a Hand-Rolled Validator to Form Requests. Two Form Requests replace one validator, and the re-approval rule moves onto the model.DELETE /offices/{id}andDELETE /offices/{id}/images/{id}: Refactoring Ergodnc - Turning Validation Errors Into Domain Exceptions. Business rules that returned 422 become exceptions that render their own 409, and file deletion moves into a queued listener.GET /offices,GET /reservationsandGET /host/reservations: Refactoring Ergodnc - Query Handlers for Laravel List Endpoints. Query handlers, Form Requests for the filters, visibility decided from the authenticated user, and the auth middleware the host listing was missing.
Problems that show up in every flow are listed once below and not repeated in each review.
What Ergodnc gets right
Model::unguard()atapp/Providers/AppServiceProvider.php:31turns off mass-assignment protection, which matches the rule that models declare neither$fillablenor$guarded, with validation at the boundary.Relation::enforceMorphMap([...])atAppServiceProvider.php:33-36keeps class names out of theimages.resource_typecolumn.Every response that carries a body goes through a
JsonResource.OfficeResourceatapp/Http/Resources/OfficeResource.php:19-22useswhenLoadedfor every relation.All five notifications implement
ShouldQueue.Six of the seven write endpoints have feature coverage; only
DELETE /reservations/{reservation}has none.itMakesReservationsattests/Feature/UserReservationControllerTest.php:163pins the discount: stays of 28 days or more get the office's monthly discount, so 40 days at 1,000 a day with 10% off comes to 36000.itMarksTheOfficeAsPendingIfDirtyattests/Feature/OfficeControllerTest.php:334pins the re-approval rule atOfficeController.php:108: changing an office'slat,lng, orprice_per_daysends it back to pending approval. The test changeslatand asserts the office is pending again. Those tests are the safety net for every rebuild in the series except cancel and the host reservations listing, which have none.Concurrency on double booking is handled with
Cache::lockatapp/Http/Controllers/UserReservationController.php:84, which most tutorial APIs I have read skip.
What breaks the rules everywhere
Code is grouped by framework type: everything sits under
app/Http/Controllers,app/Models,app/Notifications, andapp/Policies.OfficeControllerhas five actions andUserReservationControllerhas three, where the canon wants one invokable controller per endpoint.None of the 44 files under
app/opens withdeclare(strict_types=1);, none of the classes isfinal, and all nine controllers extend the framework's baseController.Production code calls facades and helpers instead of taking dependencies through the constructor. Counting files that import each facade across the controllers and the console command:
Notification5,Auth3,Cache3,Storage2,RateLimiter2,DB1,Hash1. The controllers call therequest()helper 49 times andauth()19 times, plusvalidator(),abort_unless(),throw_if(), andnow().Token abilities are checked with nine
tokenCan('...')string literals inside the controllers (UserReservationController.php:24,:54,:122;OfficeController.php:65,:98,:131;OfficeImageController.php:18,:39;HostReservationController.php:15). The canon wants ability middleware on the route with a typed constant.Business rules are thrown as validation errors. Eight rules use
ValidationException::withMessages(UserReservationController.php:67,:73,:79,:90,:129;OfficeController.php:139;OfficeImageController.php:46,:50; a ninth call atLoginController.php:39handles bad credentials), so "you cannot reserve your own office" and a malformed date both come back as the same 422.Approval and reservation statuses are integer class constants (
app/Models/Office.php:18-19,app/Models/Reservation.php:13-14) instead of enums.No domain event exists. The only mapped event in
app/Providers/EventServiceProvider.php:17-21is the framework'sRegistered, and every notification is sent inline from a controller.Every route with a parameter uses route-model binding (
routes/api.php:29,:31,:32,:35,:36,:41), so the lookup happens before any ownership check can scope it. No route has a name (routes/api.php:17-44). Login and register (routes/api.php:17-18) carry only the globalthrottle:apilimit of 60 requests a minute (app/Http/Kernel.php:44) and no stricter limiter of their own, and/host/reservationshas no auth middleware at all (routes/api.php:44), althoughHostReservationController.php:15calls a method onauth()->user().The tests use the
@testdocblock and camelCase names. PHPUnit 9 on Laravel 8 has no#[Test]attribute, so I don't count the docblock against it. What is missing matters more: no test asserts a 401 for a logged-out request, the three cancel conditions atUserReservationController.php:126-132have no test, and the 28-day discount has one test, the 40-day case atUserReservationControllerTest.php:182, with nothing on the 27-versus-28-day boundary.
The module map
The rebuild splits the app into three modules. App\Offices owns offices, images, tags, and approval. App\Reservations owns reservations, stays, and prices. App\Identity owns the User model, login, and tokens. A Shared folder holds only the QueueName enum.
Today Office::reservations() at app/Models/Office.php:35-38 and Reservation::office() at app/Models/Reservation.php:29-32 tie offices and reservations together, and the office listing filters and counts through that relation at OfficeController.php:31 and :47. The database does not: database/migrations/2021_09_09_124300_create_reservations_table.php:19 declares office_id with foreignId() and no constrained(), so no foreign key exists. After the split, office_id and user_id are plain integer columns with no relation behind them.
When one module needs something from another, it defines an interface in its own Contracts/ folder, and the module that owns the data implements it in Integrations/ and binds it in its service provider:
Reservations defines
ReservableOffices, which Offices implements inOfficesForReservations, andReservationContacts, which Identity implements inContactsForReservations.Offices defines
ActiveReservationsandOfficeReservations, which Reservations implements in one class,ReservationsForOffices, andOfficeAdmins, which Identity implements inAdminsForOffices.
Notifications go to email addresses through Laravel's on-demand notifications, so neither Offices nor Reservations imports the User model.
Reservations needs the host's id to notify them and to list a host's reservations, so it stores its own copy as reservations.host_user_id, taken from the office at booking time. An office never changes host (OfficeController.php:75 sets user_id once, and the validator has no user_id rule), so nothing has to keep the copy in sync. Existing rows are filled by a one-off BackfillReservationHosts command between two migrations, because a migration must not touch another module's table.
Before, at commit 42907c0, the code is grouped by framework type (the eight stock middleware files are collapsed):
app/
Console/
Kernel.php
Commands/
SendDueReservationsNotifications.php
Exceptions/
Handler.php
Http/
Kernel.php
Controllers/
Controller.php
HostReservationController.php
LoginController.php
LogoutController.php
OfficeController.php
OfficeImageController.php
RegisterController.php
TagController.php
UserController.php
UserReservationController.php
Middleware/
eight stock Laravel middleware
Resources/
ImageResource.php
OfficeResource.php
ReservationResource.php
TagResource.php
UserResource.php
Models/
Image.php
Office.php
Reservation.php
Tag.php
User.php
Validators/
OfficeValidator.php
Notifications/
HostReservationStarting.php
NewHostReservation.php
NewUserReservation.php
OfficePendingApproval.php
UserReservationStarting.php
Policies/
OfficePolicy.php
Providers/
AppServiceProvider.php
AuthServiceProvider.php
BroadcastServiceProvider.php
EventServiceProvider.php
RouteServiceProvider.php
routes/
api.phpAfter the rebuild, with the slices the five reviews build:
app/
Identity/
IdentityServiceProvider.php
Integrations/
AdminsForOffices.php
ContactsForReservations.php
User/
User.php
Offices/
OfficeAbility.php
OfficesException.php
OfficesServiceProvider.php
offices-routes.php
Contracts/
ActiveReservations.php
OfficeAdmins.php
OfficeReservations.php
Integrations/
OfficesForReservations.php
Files/
OfficeImageFiles.php
Image/
Image.php
Tag/
Tag.php
Office/
Office.php, OfficeFactory.php, OfficeApprovalStatus.php,
OfficeDetails.php, OfficeChanges.php, domain exceptions
SubmitOffice/
ReviseOffice/
DeleteOffice/
DeleteOfficeImage/
ListOffices/
Reservations/
ReservationAbility.php
ReservationsException.php
ReservationsServiceProvider.php
reservations-routes.php
BackfillReservationHosts/
BackfillReservationHosts.php
Contracts/
ReservableOffice.php
ReservableOffices.php
ReservationContacts.php
Integrations/
ReservationsForOffices.php
Reservation/
Reservation.php, ReservationFactory.php, ReservationStatus.php,
ReservationLock.php, Stay.php, StayPrice.php, domain exceptions
ReserveOffice/
CancelReservation/
ListGuestReservations/
ListHostReservations/
Shared/
Queue/
QueueName.php
bootstrap/
app.php
providers.php
database/migrations/
2026_10_12_000001_add_host_user_id_to_reservations_table.php
2026_10_12_000002_make_host_user_id_required_on_reservations_table.php
2026_10_12_000003_convert_reservation_status_to_string.php
2026_10_12_000004_convert_office_approval_status_to_string.phpEach module's service provider loads its route file inside the api middleware group with no URL prefix. The canon says module routes load under the api prefix, but ergodnc serves its API at the root path (app/Providers/RouteServiceProvider.php sets no prefix, and every test uses root paths), and adding the prefix would change every URL the reviews quote.
Open items
Ids stay enumerable. Offices, images, and reservations are addressed by their auto-increment primary key (
routes/api.php:29-41), which the canon forbids. Switching to ULIDs touches every route, resource, and factory, and none of the flows needs it to make its point.The reviews do not show tests. The existing feature tests assert the old 422s and the old 200 on delete, so each rebuild changes them.
SendDueReservationsNotificationsatapp/Console/Commands/SendDueReservationsNotifications.phpis not rebuilt. It callsnow()andNotification::sendinhandle()(lines 47-50) and still has the scaffold description "Command description" at line 25.Login, register, logout, and the tags endpoint are untouched, so their findings above stay open.
The image upload,
POST /offices/{office}/imagesatroutes/api.php:35, is not rebuilt.
Related Links
- Refactoring Ergodnc - Moving Business Logic Out of a Laravel ControllerPOST /reservations rebuilt as a vertical slice with a domain model
- Refactoring Ergodnc - Putting the Ownership Check Inside the LookupDELETE /reservations/{id} with the ownership check in the model lookup
- Refactoring Ergodnc - From a Hand-Rolled Validator to Form RequestsPOST /offices and PUT /offices/{id} with Form Requests and the re-approval rule on the model
- Refactoring Ergodnc - Turning Validation Errors Into Domain Exceptionsthe two delete endpoints with exceptions that render their own 409
- Refactoring Ergodnc - Query Handlers for Laravel List Endpointsthe office, guest and host listings as read slices
- themsaid/ergodnc at commit 42907c0the code this review reads