The N+1 query problem is the most common silent performance bug in Laravel applications: a page that looks instant on your machine with ten records issues hundreds of queries in production with ten thousand. Laravel ships with a built-in tripwire - Model::preventLazyLoading() - that turns every hidden lazy load into a loud exception during development, so the bug never ships.
What an N+1 looks like
$orders = Order::all(); // 1 query
foreach ($orders as $order) {
echo $order->customer->name; // 1 query PER order
}
With 500 orders that is 501 queries. Each $order->customer access quietly runs select * from customers where id = ? because the relationship was not loaded up front.
Turn on the tripwire
In App\Providers\AppServiceProvider::boot():
use Illuminate\Database\Eloquent\Model;
public function boot(): void
{
Model::preventLazyLoading(! $this->app->isProduction());
}
Now any lazy load outside production throws LazyLoadingViolationException naming the exact model and relationship:
Illuminate\Database\LazyLoadingViolationException:
Attempted to lazy load [customer] on model [App\Models\Order]
but lazy loading is disabled.
The exception message is your fix instruction: eager-load that relationship.
Fix the violations it finds
Eager-load with with() at query time, or loadMissing() when you already hold the models:
Order::with('customer')->get(); // 2 queries total
Order::with('customer.address')->get(); // nested relationships
$orders->loadMissing('customer'); // after the fact
Keep the guard off in production (! $this->app->isProduction()): an uncaught exception on a live page is worse than a slow query. In production you want the query log, not the crash.
Find the lazy loads you never click
Manual browsing only exercises the paths you remember. Two habits close the gap:
1. Run the test suite. Feature tests that render pages or hit API endpoints will trip the exception on every hidden lazy load they touch. A red test names the missing with() for you.
2. Watch production instead of crashing it. Log slow or repeated queries with DB::listen() or a tool like Laravel Pulse, Telescope (locally), or your APM. Sort by query count per request - the N+1s stand out immediately.
The companion guards
While you are in AppServiceProvider, two sibling protections are worth the same one-line cost:
Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
Model::preventAccessingMissingAttributes(! $this->app->isProduction());
The first throws when a non-fillable attribute is silently dropped on mass assignment; the second throws when you read an attribute you never selected - a common source of "works locally, null in production" bugs after someone adds a select() to a query.
Summary
Model::preventLazyLoading() converts the most expensive query pattern in Laravel from a production incident into a development exception. Enable it outside production, let your tests surface the violations, and fix each one with with(). Your database will notice the difference before your users do.