laravel-eloquent-performance
Skill1 starsChanged 7 months agoMITView on GitHub
Find and fix Eloquent performance problems - N+1 queries, eager loading strategy, chunking, indexes, and query review for Laravel apps. Use when a Laravel page or job is slow, when reviewing Eloquent code for query efficiency, when the user mentions N+1 problems, or before shipping list views and reports that touch large tables.
In this repository50 other files it gives its agents.
- accessibility-reviewSkill
- adr-authorSkill
- api-designSkill
- architecture-reviewSkill
- caching-strategySkill
- changelogSkill
- ci-cd-pipelineSkill
- codebase-explorationSkill
- code-reviewSkill
- code-style-guideSkill
- context-engineeringSkill
- conventional-commitsSkill
- cost-optimizationSkill
- database-migrationSkill
- data-privacy-reviewSkill
- debuggingSkill
- dependency-updateSkill
- deprecation-migrationSkill
- error-handlingSkill
- feature-flag-rolloutSkill
- filament-resource-designSkill
- flux-ui-patternsSkill
- git-workflowSkill
- i18n-reviewSkill
- idea-refineSkill
- incident-responseSkill
- incremental-implementationSkill
- inertia-patternsSkill
- laravel-ai-featuresSkill
- laravel-queue-designSkill
- laravel-testingSkill
- livewire-component-designSkill
- llm-feature-designSkill
- observabilitySkill
- onboarding-docSkill
- performance-budgetSkill
- pr-descriptionSkill
- primevue-patternsSkill
- refactoringSkill
- release-checklistSkill
- resilience-patternsSkill
- runbook-authorSkill
- security-auditSkill
- spec-writingSkill
- task-breakdownSkill
- tech-debt-triageSkill
- test-writingSkill
- threat-modelingSkill
- using-elyra-skillsSkill
- vue-component-patternsSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 22
Before shipping anything that touches large tables
- Very specific: line 82
Every column in where/orderBy/joins on big tables: candidate
- Very specific: line 84
Verify with DB::select('EXPLAIN ...') — look for full table scans
- Very specific: line 89
Keep Model::shouldBeStrict() in non-production permanently
- Distinctive: line 90
Add a CI-friendly assertion where it matters: e.g.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Laravel Eloquent Performance
Eloquent makes queries invisible — which is how one Blade loop quietly becomes 300 of them. Make queries visible first, then fix the worst offender, then prevent recurrence.
When to use
- A Laravel page, endpoint, or job is slow
- Reviewing code that loops over models or renders collections
- "We have an N+1 problem" / preparing list views, exports, dashboards
- Before shipping anything that touches large tables
Principles
- Measure before touching. Count queries and duration; don't guess from code shape.
- The database does sets; PHP does rows. Filtering, counting, and aggregating in a
->filter()/->count()after->get()is the cardinal sin. - Load what the view needs, nothing more. Eager load relations the page renders; select the columns it shows.
- Prevention beats heroics. One strict-mode line catches future N+1s in development forever.
Process
1. Make queries visible
// AppServiceProvider::boot() — development
Model::shouldBeStrict(! app()->isProduction()); // throws on lazy loading, silent attribute access
DB::listen(fn ($q) => logger()->debug($q->sql, ['ms' => $q->time]));Or use Debugbar/Telescope/->dd() on the query log for the specific request. Record: query count + total ms as the baseline.
2. Fix N+1 (the usual suspect)
// Symptom: query per row in a loop
$posts = Post::with(['author', 'comments.user'])->get(); // eager load
$posts = Post::withCount('comments')->get(); // counts without loading
$query->withAvg('reviews', 'rating'); // aggregates likewise- Nested relations:
with('comments.user'), constrained:with(['comments' => fn ($q) => $q->latest()->limit(5)]) - In Blade components rendered per-row: the N+1 hides in the component — eager load in the parent query
3. Stop over-fetching
->select(['id', 'title', 'author_id'])— include FKs needed bywith()- Big text/blob columns excluded from list queries
exists()notcount() > 0;value('col')notfirst()->col
4. Move work to the database
| PHP smell | Database fix |
->get()->filter(...) | ->where(...) |
->get()->count() | ->count() |
->get()->sum('x') | ->sum('x') |
->get()->groupBy(...) | ->groupBy() + aggregate, or a dedicated query |
5. Handle large datasets
- Iteration:
chunkById()(notchunk()when mutating rows) orlazyById()/cursor() - Exports/jobs: never
all()on an unbounded table - Pagination:
paginate()for UI;cursorPaginate()for infinite scroll / large offsets
6. Check indexes
php artisan db:table <table> # see existing indexes- Every column in
where/orderBy/joins on big tables: candidate - Composite index order: equality columns first, then range/sort
- Verify with
DB::select('EXPLAIN ...')— look for full table scans
7. Re-measure and lock in
- Compare query count + ms against baseline; report both
- Keep
Model::shouldBeStrict()in non-production permanently - Add a CI-friendly assertion where it matters: e.g. test that a page renders in ≤ N queries
Output format
## Eloquent performance: <page/endpoint/job>
**Baseline:** N queries / X ms → **After:** M queries / Y ms
### Fixes
1. <file:line> — <problem> → <fix>
### Indexes added/proposed
- <table>.<cols> — for <query>
### Prevention
- strict mode: on | query-count test: added | …Anti-patterns
- ❌ Sprinkling
with()everywhere "just in case" — eager loading unrendered relations is its own waste - ❌ Caching as the first response to a slow query instead of fixing the query
- ❌
chunk()while updating the column you chunk by (rows get skipped — usechunkById()) - ❌ Fixing the N+1 in the controller while the Blade component re-introduces it
- ❌ Adding indexes without checking they're used (
EXPLAIN) - ❌ Declaring victory without before/after numbers
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

