title: Laravel eager loading deep relationships
body: <p>I've hit a bit of a snag with Laravel and eager loading relationships. I have three models <code>AssetFolder</code>, <code>Asset</code> &amp; <code>DownloadLog</code></p>
  
  <ul>
  <li><code>AssetFolder</code> HasMany <code>Asset</code> via <code>assets</code></li>
  <li><code>Asset</code> HasMany <code>DownloadLog</code> via <code>downloads</code></li>
  </ul>
  
  <p>I'm currently querying for a particular asset folder, and in doing so I'm eager loading the assets.</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$folder = AssetFolder::where('name', 'Example')
      -&gt;with([
          'assets' =&gt; function (HasMany $query) {
              $query-&gt;orderBy('name', 'asc');
          }
      ])-&gt;first();
  </code></pre>
  
  <p>This part works great. But for the purposes of this query, I'd also like to eager load each <code>Asset</code> associated <code>DownloadLog</code> records. I've tried solution #1 - </p>
  
  <pre class="lang-php prettyprint-override"><code>$folder = AssetFolder::where('name', 'Corporate Assets')
      -&gt;with([
          'assets' =&gt; function (HasMany $query) {
              $query-&gt;orderBy('name', 'asc');
          },
          'assets.downloads'
      ])-&gt;first();
  </code></pre>
  
  <p>I'd now expect my <code>Asset</code> model to contain a relationship called <code>downloads</code> full of <code>DownloadLog</code> objects but unforunately that wasn't the case. The <code>downloads</code> relationship wasn't even referenced. So then I tried solution #2 - </p>
  
  <pre class="lang-php prettyprint-override"><code>$folder = AssetFolder::where('name', 'Corporate Assets')
      -&gt;with([
          'assets' =&gt; function (HasMany $query) {
              $query
                  -&gt;with('downloads')
                  -&gt;orderBy('name', 'asc');
          }
      ])-&gt;first();
  </code></pre>
  
  <p>This didn't work either - same result as above. Now I would have expected that to work but it didn't. So to test, I went into my <code>AssetFolder</code> model and updated my HasMany relationship method to the following - </p>
  
  <pre class="lang-php prettyprint-override"><code>public function assets()
  {
      return $this
          -&gt;hasMany(Asset::class)
          -&gt;with('downloads');
  }
  </code></pre>
  
  <p>And that works! That's great, but the problem is that 90% of the time, I don't want to pull the download logs along with the assets I'm loading. That's why I thought solution #2 would work because I thought it simply altered the HasMany query.</p>
  
  <p>Am I doing something wrong here?</p>
  
  <p>Chris.</p>