Preventing Laravel N+1 Queries with Lazy-Loading ProtectionPreventing Laravel N+1 Queries with Lazy-Loading Protection
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
A Laravel habit that has saved me more debugging time than any package: turning on lazy-loading protection in development. 🛠️
In AppServiceProvider::boot() I add Model::preventLazyLoading(! app()->isProduction()). Now whenever a Blade view, Livewire component or API resource quietly triggers an N+1 query, Laravel throws an exception locally instead of letting it slip into production.
The fix is usually simple: eager load with with() or load(), use withCount() instead of counting a relation inside a loop, and select only the columns the page actually needs.
I pair it with Debugbar or Telescope so I can see the query count per request before a feature ships. On multi-tenant apps like CRMs, where one list page can touch several relations, it catches problems early.
If your Laravel app feels slower than it should, I'm happy to take a look.
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started