<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chathura Rathnayaka</title>
    <description>The latest articles on DEV Community by Chathura Rathnayaka (@prabashanadev).</description>
    <link>https://dev.to/prabashanadev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3966191%2F8535db46-941a-405f-a7a6-e596bb901c4b.jpg</url>
      <title>DEV Community: Chathura Rathnayaka</title>
      <link>https://dev.to/prabashanadev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prabashanadev"/>
    <language>en</language>
    <item>
      <title>Daily Dev Dive: Unmask the N+1 Villain with Eager Loading!</title>
      <dc:creator>Chathura Rathnayaka</dc:creator>
      <pubDate>Wed, 03 Jun 2026 09:35:20 +0000</pubDate>
      <link>https://dev.to/prabashanadev/daily-dev-dive-unmask-the-n1-villain-with-eager-loading-4g1n</link>
      <guid>https://dev.to/prabashanadev/daily-dev-dive-unmask-the-n1-villain-with-eager-loading-4g1n</guid>
      <description>&lt;h1&gt;
  
  
  Is Your Laravel App Secretly Sluggish? Unmask the N+1 Villain with Eager Loading!
&lt;/h1&gt;

&lt;p&gt;Ever found yourself tapping your fingers, waiting for an application to load, or watching a spinner spin endlessly? As developers, we've all been there – both as users and, sometimes, as the unwitting architects of those delays. Whether you're building a sleek frontend with Flutter, a robust backend with Laravel, or fortifying systems against cyber threats, performance is paramount. A slow application doesn't just annoy users; it can degrade security, impact scalability, and ultimately, cost you valuable resources.&lt;/p&gt;

&lt;p&gt;Today, we're diving deep into a notorious performance killer in the Laravel ecosystem: the dreaded N+1 query problem. But fear not, we'll arm you with the ultimate weapon to combat it: &lt;strong&gt;Eager Loading&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The N+1 Nightmare: A Hidden Performance Drain
&lt;/h2&gt;

&lt;p&gt;Imagine you have a blog application. You want to display a list of &lt;code&gt;Posts&lt;/code&gt;, and for each post, you also want to show its &lt;code&gt;Comments&lt;/code&gt;. A common, yet dangerously inefficient, way to do this might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The N+1 problem in action:&lt;/span&gt;
&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Query 1: Fetch all posts&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;comments&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Query N (for N posts): Fetch comments for each post&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$comment&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening here?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Laravel fetches all &lt;code&gt;Post&lt;/code&gt; records (1 query).&lt;/li&gt;
&lt;li&gt; Then, for &lt;em&gt;each&lt;/em&gt; of those &lt;code&gt;Post&lt;/code&gt; records, it executes a &lt;em&gt;separate&lt;/em&gt; query to fetch its associated &lt;code&gt;Comments&lt;/code&gt;. If you have 100 posts, that's 1 (for posts) + 100 (for comments) = 101 database queries!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This &lt;code&gt;N+1&lt;/code&gt; pattern generates an astronomical number of queries, saturating your database, exhausting resources, and turning your blazing-fast application into a sluggish crawl. This isn't just a Laravel problem; inefficient data fetching plagues all systems. If your Flutter app is hitting an API that has an N+1 problem, your mobile experience will suffer. From a cybersecurity perspective, resource exhaustion from excessive queries can even make your application vulnerable to denial-of-service (DoS) attacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hero We Deserve: Eager Loading with &lt;code&gt;with()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The elegant solution to the N+1 problem is &lt;strong&gt;eager loading&lt;/strong&gt;. Instead of loading related models one by one, eager loading tells Laravel to fetch all necessary related data in advance, drastically reducing the number of database queries. The magic method is &lt;code&gt;with()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's rewrite our problematic example using eager loading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Eager Loading to the rescue!&lt;/span&gt;
&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'comments'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Query 1: Fetch all posts AND Query 2: Fetch all related comments&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;comments&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Comments are already loaded! No new queries here.&lt;/span&gt;
        &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$comment&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the difference? With &lt;code&gt;Post::with('comments')-&amp;gt;get()&lt;/code&gt;, Laravel performs only &lt;em&gt;two&lt;/em&gt; queries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; One query to fetch all the &lt;code&gt;posts&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; A second, highly optimized query to fetch &lt;em&gt;all&lt;/em&gt; &lt;code&gt;comments&lt;/code&gt; that belong to those posts, based on their foreign keys.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then, when you iterate through &lt;code&gt;$post-&amp;gt;comments&lt;/code&gt;, the relationships are already "hydrated" in memory, requiring no further database trips. This simple change transforms 101 queries into a mere 2 queries, delivering a performance boost you can truly feel.&lt;/p&gt;

&lt;p&gt;Beyond simple relationships, &lt;code&gt;with()&lt;/code&gt; supports nested eager loading (&lt;code&gt;with('comments.user')&lt;/code&gt;), conditional eager loading (&lt;code&gt;with(['comments' =&amp;gt; function ($query) { ... }])&lt;/code&gt;), and even counting related models (&lt;code&gt;withCount('comments')&lt;/code&gt;) without loading them all. Mastering &lt;code&gt;with()&lt;/code&gt; is a cornerstone of building scalable, high-performance Laravel applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build Blazing-Fast &amp;amp; Secure Applications
&lt;/h2&gt;

&lt;p&gt;Understanding and implementing eager loading is a fundamental skill that transcends specific frameworks. It's about intelligent resource management, a concept vital whether you're optimizing database calls in Laravel, structuring API requests for your Flutter frontend, or ensuring your backend systems are robust and efficient enough to withstand performance attacks.&lt;/p&gt;

&lt;p&gt;So, next time you're fetching related models, remember the N+1 villain. Prioritize eager loading with &lt;code&gt;with()&lt;/code&gt; and watch your application supercharge its performance, provide a smoother user experience, and stand resilient against the strains of high traffic. Your users, and your server, will thank you!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>learning</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
