The N+1 is the bug that ships green. It passes code review, passes your tests, runs fine in staging with a handful of seed rows, then degrades in production on a curve nobody's watching, because the cost is one extra query per row and your rows only grow. By the time a page is visibly slow, the N+1 has usually been there for months. That's the case for catching it three ways: a tripwire in development so new ones can't ship, the fix once you find one, and monitoring in production so the ones that slipped through surface as data instead of complaints.
1. Reproduce it locally
The fastest way to spot an N+1 is to count queries per request. Laravel Telescope (official, free) or laravel-debugbar both expose this.
composer require laravel/telescope --dev
php artisan telescope:install
php artisan migrate
Installing with --dev means production won't have the package, so follow the local-only steps in the Telescope docs and register its service provider only in the local environment.
Load the slow page, then open /telescope/queries. If one endpoint fires 50+ similar queries, you have an N+1.
2. Fail loud in development
Eloquent lets you throw an exception whenever a relationship is lazy-loaded. This surfaces N+1s at the moment they're introduced, not weeks later when the page is slow.
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Strict in dev and staging. Silent in production so we don't break
// real users. We'll monitor N+1s in prod with an APM instead.
Model::preventLazyLoading(! app()->isProduction());
}
}
Now lazy loading a relation in development throws LazyLoadingViolationException with the exact relationship name. It only throws for models that came back from a multi-row result, which is where N+1s live; a model fetched on its own can still lazy load. Your tests will catch it too.
3. Fix it with eager loading
The idiomatic fix is with() at query time or load() after the fact.
Before: fires 51 queries for 50 posts
$posts = Post::latest()->take(50)->get();
foreach ($posts as $post) {
echo $post->author->name; // 1 query each, 50 extra queries
}After: fires 2 queries
$posts = Post::with('author')->latest()->take(50)->get();
foreach ($posts as $post) {
echo $post->author->name; // in-memory, no query
}
For nested relationships, use dotted syntax. Polymorphic morphTo relations eager load with a plain with(); reach for morphWith() only when each morph type needs its own nested relations. For column-selected eager loads:
Post::with([
'author:id,name,avatar',
'comments' => fn($q) => $q->latest()->limit(3),
'tags',
])->paginate(25);4. Catch them in production
preventLazyLoading only helps in environments you actively test. In production you need an APM that records the queries real requests run, so repeated patterns show up in the data.
Laravel Telescope is not production-grade: it stores every entry in your app's database (after the response, but in the same PHP process), has no aggregation across requests, and outside the local environment its default filter keeps only exceptions, failed requests, failed jobs, scheduled tasks and tagged entries. Options that are:
- Laravel Nightwatch Cloud: official, records every query with trace context
- NightOwl: BYOD Postgres dashboard on the same Nightwatch package
- Sentry Performance: generic APM, weaker at Laravel-specific patterns