Skip to content
Daily Dev Tip

This tip lives in your new tab — for free.

Install for Chrome →
LARAVEL

Turn N+1 queries into errors in development with preventLazyLoading()

app/Providers/AppServiceProvider.php
php
public function boot(): void
{
    Model::preventLazyLoading(! app()->isProduction());
}

Called in a service provider, this throws a `LazyLoadingViolationException` whenever a relation is accessed without being eager-loaded. N+1 problems are invisible in code review and only show up as latency under load; this surfaces them the moment you hit the endpoint locally. Gate it to non-production so you never break a live request — the goal is to force `with()`/`load()` during development, not in front of users.

eloquentperformancen+1debugging