php
30 lines · 7 steps
Catching N+1 queries with Eloquent strict mode in Laravel
Configure Eloquent to fail loudly on lazy loading during development while staying quiet in production.
Explained by
highlit
1namespace App\Providers;
2
3use Illuminate\Database\Eloquent\Model;
4use Illuminate\Support\Facades\Log;
5use Illuminate\Support\ServiceProvider;
6
7class AppServiceProvider extends ServiceProvider
8{
9 public function boot(): void
10 {
11 Model::preventLazyLoading(! $this->app->isProduction());
12
13 Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
14
15 Model::handleLazyLoadingViolationUsing(function (Model $model, string $relation) {
16 $class = get_class($model);
17
18 Log::channel('stack')->warning("Lazy loading [{$relation}] on [{$class}].");
19
20 if ($this->app->runningInConsole()) {
21 return;
22 }
23
24 throw new \RuntimeException(
25 "Attempted to lazy load [{$relation}] on model [{$class}]. "
26 . 'Eager load it with ->with() to avoid an N+1 query.'
27 );
28 });
29 }
30}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Turning lazy-loading violations into exceptions surfaces N+1 problems at development time instead of in production traffic.
- 2Gating strict behaviors on non-production keeps guardrails from breaking live requests.
- 3A custom violation handler lets you log context and choose whether to warn or throw per environment.
Related explainers
php
<?php namespace App\Http\Controllers;
Server-Sent Events in Laravel
server-sent-events
streaming
long-polling
Advanced
9 steps
php
<?php namespace App\Actions\Imports;
Streaming CSV imports with Laravel batches
lazy-collections
job-batching
streaming
Advanced
9 steps
php
<?php namespace App\Support;
Recursively finding files with SPL iterators in PHP
recursion
iterators
filesystem
Intermediate
7 steps
php
<?php namespace App\Http\Requests;
A validated date-range value object in PHP
value-object
validation
immutability
Intermediate
7 steps
php
<?php namespace App\Http\Controllers\Api;
Building a cursor-paginated feed in Laravel
cursor-pagination
eager-loading
validation
Intermediate
8 steps
php
<?php namespace App\Services;
Caching per-tenant settings in Laravel
caching
multi-tenancy
dependency-injection
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/catching-n-1-queries-with-eloquent-strict-mode-in-laravel-explained-php-e828/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.