title: Laravel - Eager Loading Polymorphic Relation's Related Models
body: <p>I can eager load polymorphic relations/models without any n+1 issues. However, if I try to access a model related to the polymorphic model, the n+1 problem appears and I can't seem to find a fix. Here is the exact setup to see it locally:</p>
  
  <p><strong>1) DB table name/data</strong></p>
  
  <p><code>history</code></p>
  
  <p><img src="https://i.stack.imgur.com/EbbDe.jpg" alt="history table"><br><br>
      <code>companies</code></p>
  
  <p><img src="https://i.stack.imgur.com/Nlawc.jpg" alt="enter image description here"><br><br>
      <code>products</code></p>
  
  <p><img src="https://i.stack.imgur.com/iEwgU.jpg" alt="enter image description here"><br><br>
      <code>services</code></p>
  
  <p><img src="https://i.stack.imgur.com/c3jWU.jpg" alt="enter image description here"><br><br></p>
  
  <p><strong>2) Models</strong></p>
  
  <pre><code>// History
  class History extends Eloquent {
      protected $table = 'history';
  
      public function historable(){
          return $this-&gt;morphTo();
      }
  }
  
  // Company
  class Company extends Eloquent {
      protected $table = 'companies';
  
      // each company has many products
      public function products() {
          return $this-&gt;hasMany('Product');
      }
  
      // each company has many services
      public function services() {
          return $this-&gt;hasMany('Service');
      }
  }
  
  // Product
  class Product extends Eloquent {
      // each product belongs to a company
      public function company() {
          return $this-&gt;belongsTo('Company');
      }
  
      public function history() {
          return $this-&gt;morphMany('History', 'historable');
      }
  }
  
  // Service
  class Service extends Eloquent {
      // each service belongs to a company
      public function company() {
          return $this-&gt;belongsTo('Company');
      }
  
      public function history() {
          return $this-&gt;morphMany('History', 'historable');
      }
  }
  </code></pre>
  
  <p><strong>3) Routing</strong></p>
  
  <pre><code>Route::get('/history', function(){
      $histories = History::with('historable')-&gt;get();
      return View::make('historyTemplate', compact('histories'));
  });
  </code></pre>
  
  <p><strong>4) Template with n+1 logged only becacuse of $history->historable->company->name, comment it out, n+1 goes away.. but we need that distant related company name:</strong></p>
  
  <pre><code>@foreach($histories as $history)
      &lt;p&gt;
          &lt;u&gt;{{ $history-&gt;historable-&gt;company-&gt;name }}&lt;/u&gt;
          {{ $history-&gt;historable-&gt;name }}: {{ $history-&gt;historable-&gt;status }}
      &lt;/p&gt;
  @endforeach
  {{ dd(DB::getQueryLog()); }}
  </code></pre>
  
  <p>I need to be able to load the company names eagerly (in a single query) as it's a related model of the polymorphic relation models <code>Product</code> and <code>Service</code>.
  I’ve been working on this for days but can't find a solution.
  <code>History::with('historable.company')-&gt;get()</code> just ignores the <code>company</code> in <code>historable.company</code>.
  What would an efficient solution to this problem be?</p>