Appearance
Database & Eloquent (project conventions)
- Casts via the
casts()method + ide-helper annotations (seephp-laravel.md). - Named scopes for domain queries (aggressive) — a
scopeForDateso call sites readPlan::forDate($today), not::where('plan_date', $today). For "the latest related row per parent" use ahasOne(...)->latestOfMany('col')relation and eager-load it — never a method that re-queries inside a loop, and neverlimit(1)on an eager-loadedhasMany(it limits total rows, not per parent):phppublic function latestRun(): HasOne { return $this->hasOne(Run::class)->latestOfMany('started_at'); } // usage: Job::with('latestRun')->get(); // one query, not N+1 - N+1s are bugs to fix aggressively — not a Tier-3 nicety. Any loop touching a relation is suspect. Eager-load with
with(), and add a query-count regression test so it can't creep back:phpDB::enableQueryLog(); // ...exercise the code... expect(DB::getQueryLog())->toHaveCount(2); // bounded, not N+1 - Migrations: one logical change each;
foreignId()->constrained(); precise types (decimalfor money, neverfloat); index what you filter/sort/join on; realdown(). Add credential/secret columns with anencryptedcast (and$hidden— seesecurity.md). - Wrap multi-step writes in
DB::transaction(...).