title: Eager loading nested relationships using pivot
body: <p>I have 4 tables currently : </p>
  
  <pre><code>accessories 
  id 
  
  products 
  id
  
  product_accessory 
  id 
  product_id
  accessory_id 
  
  product_accessory_adaptor
  id 
  product_accessory_id 
  </code></pre>
  
  <p>Mapping them to Eloquent models gives me</p>
  
  <p>Custom pivot model : </p>
  
  <pre><code>class ProductAccessory extends Pivot {
     protected $table = 'product_accessory';
  
     public function product()
     {
        return $this-&gt;belongsTo('Product');
     }
  
     public function accessory()
     {
       return $this-&gt;belongsTo('Accessory');
     }
  
     public function adaptors() {
       return $this-&gt;hasMany('Adaptor', 'product_accessory_id'); 
     } 
  }
  </code></pre>
  
  <p>Product and Accessory model</p>
  
  <pre><code>class Accessory extends Eloquent {
  
     public function products()
     {
        return $this-&gt;belongsToMany('Product', 'product_accessory', 'accessory_id', 'product_id')-&gt;withPivot();
     }
  
     public function newPivot(Eloquent $parent, array $attributes, $table, $exists) 
     {
        if ($parent instanceof Product) {
            return new ProductAccessory($parent, $attributes, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $exists);
     }
  
     public function adaptors()
     {
        return $this-&gt;hasManyThrough('Adaptor', 'ProductAccessory', 'accessory_id', 'product_accessory_id');
     }
  }
  
  class Product extends Eloquent {
  
     public function accessories()
     {
        return $this-&gt;belongsToMany('Accessory', 'product_accessory', 'product_id', 'accessory_id')-&gt;withPivot();
     }
  
     public function newPivot(Eloquent $parent, array $attributes, $table, $exists) 
     {
        if ($parent instanceof Accessory) {
            return new ProductAccessory($parent, $attributes, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $exists);
     }
  
     public function adaptors()
     {
        return $this-&gt;hasManyThrough('Adaptor', 'ProductAccessory', 'product_id', 'product_accessory_id');
     }
  }
  </code></pre>
  
  <p>Adaptor model: </p>
  
  <pre><code>class Adaptor extends Eloquent {
  
     protected $table = 'product_accessory_adaptor';
  
     public function productAccessory() {
        return $this-&gt;belongsTo('ProductAccessory');
     }
  }
  </code></pre>
  
  <p>Here's what i need. Eager loading all products with accessories with adaptors belonging to their pivot. I've tried something like this but i have no clue how to pass in the pivot id </p>
  
  <p>in Product Model </p>
  
  <pre><code>public function accessories()
  {
      return $this-&gt;belongsToMany('Compatibility\Accessory', 'product_accessory', 'product_id', 'accessory_id')
                  -&gt;withPivot('id', 'accessory_disclaimer')
                  -&gt;withTimestamps()
                  -&gt;with(['adaptors' =&gt; function ($q) {
                      $q-&gt;where('product_accessory_id', $this-&gt;pivot-&gt;id);                       
                  }]);
  }
  </code></pre>
  
  <p>Does anyone has any clue how to do this? What should i replace with $this->pivot->id?</p>