Let Claude Fix Your Eloquent N+1 Query Problems
N+1 queries are one of the most common Laravel performance killers and they're easy to miss — your code looks fine but you're firing hundreds of database queries on every request.
Paste your controller or service method and ask Claude to spot and fix them:
@UserController.php @PostController.php
Find any N+1 query problems in these files and fix them
with eager loading. Explain what each one was doing.
Claude will rewrite your queries with the right with() or load() calls and explain exactly why each change matters:
// Before — fires one query per user to get their posts
$users = User::all();
foreach ($users as $user) {
echo $user->posts->count();
}
// After — two queries total, regardless of user count
$users = User::with('posts')->get();
foreach ($users as $user) {
echo $user->posts->count();
}
You can also ask Claude to go further and add nested eager loading for deeper relationships, or switch to withCount() when you only need totals:
Now check for nested N+1s in the relationships too,
and use withCount() where we only need the count.
If you have Laravel Debugbar or Telescope installed, paste the query log output alongside your code — Claude can match up the duplicate queries to the exact lines causing them.
N+1 problems compound fast at scale — catch them in review, not in production.
Log in to leave a comment.
When a page takes five seconds to load or an API endpoint times out under load, tell Claude which route is slow and it traces the entire code path — controller, services, queries, loops — identifying N+1 queries, redundant computations, missing indexes, and cacheable operations, then fixes each bottleneck.
Flaky tests are maddening — they pass locally, fail in CI, pass again when you retry. Tell Claude to read the test, identify the source of non-determinism — timing issues, shared state, date dependencies, or order-dependent setup — and fix the root cause so the test is reliably green or reliably red.
When your app throws an error, don't just Google the message — paste the full stack trace into Claude Code. It reads the trace, opens the referenced files in your codebase, follows the call chain, and pinpoints the actual root cause instead of just explaining the symptom.