Currently accepting 2 new clients for Q2 2026 β€” book a call or send a brief to hold your slot.
Web Development

Laravel n+1 query optimisation fix

Arun Tyagi
April 09, 2026
8 views

The Problem: Slow SaaS Application & High Database Load



While working on a SaaS application, I noticed something critical β€” pages that should load in milliseconds were taking 2–3 seconds.


At first glance, everything looked fine. Clean code, optimized queries, proper indexing. But the database load told a different story.


After deeper inspection, the issue was clear:

πŸ‘‰ Laravel N+1 Query Problem





πŸ” What is the N+1 Problem in Laravel?



The N+1 problem occurs when your application executes:


  • 1 query to fetch main records
  • N additional queries to fetch related data


Example 

$users = User::all();

foreach ($users as $user) {
    echo $user->posts;
}


This results in:


  • 1 query for users
  • N queries for posts



πŸ‘‰ Total = N+1 queries (bad for performance)





⚠️ Real Impact (Production Case)



In my SaaS project:


  • Users loaded: 100
  • Queries executed: 101+
  • Page load time: 2.4 seconds



This was a serious scalability issue.





βœ… Step 1: Diagnose Using Laravel Debugbar



To confirm the issue, I used Laravel Debugbar.


It clearly showed:


  • Repeated queries
  • Duplicate database calls
  • High query count



This is the fastest way to detect N+1 issues in development.





⚑ Step 2: Fix Using Eager Loading (

with()

)



The solution is simple but powerful β€” Eager Loading.

$users = User::with('posts')->get();

foreach ($users as $user) {
    echo $user->posts;
}


Now Laravel fetches everything in just 2 queries:


  • 1 for users
  • 1 for posts






πŸ“Š Before vs After Optimization



 | Scenario | Query Count | Load Time
| Before Fix | 101+ queries | 2.4 sec
| After Fix | 2 queries | 400 ms


πŸ‘‰ ~90% reduction in queries





🧠 Pro Tips for Advanced Optimization



  • Always use with() for relationships
  • Use withCount() when only counts are needed
  • Avoid lazy loading in loops
  • Use query logging in production debugging
  • Combine with caching for maximum performance






πŸ”— When to Worry About N+1



You should fix this immediately if:


  • Your app is slow under load
  • Queries increase with data size
  • Server CPU usage is high






πŸ’Ό Need Help with Laravel Performance?



If you’re facing performance issues, slow APIs, or scaling problems β€” I can help.


πŸ‘‰ Check my Laravel development services here:

https://aruntyagi.com/laravel-development


I specialize in:


  • Performance optimization
  • SaaS architecture
  • Scalable backend systems
  • AI-powered development






🏁 Conclusion



The N+1 problem is one of the most common β€” and most damaging β€” performance issues in Laravel applications.


The good news?

πŸ‘‰ It’s easy to fix once you know where to look.


By applying eager loading and proper debugging, you can drastically improve performance and scalability.