title: join a table in two columns due to polymorphic relation
body: <p>I have the following structure:</p>
  
  <p><strong>Buro->hasOne->Address->hasOne->City->hasOne->State
  Buro->hasOne->Category</strong></p>
  
  <p>and</p>
  
  <p><strong>Order->hasOne->Address->hasOne->City->hasOne->State
  Order->hasOne->Category</strong></p>
  
  <p>and</p>
  
  <p><strong>Category->hasMany->Orders and Category->hasMany->Buros</strong></p>
  
  <p>I have a "One-To-Many" polymorphic relation between addresses in one side and Buro/Orders in the other side. There is just One address for a buro and just one adddress for an order, therefor I couls use [0] to get the first the first and only one element in a collection.</p>
  
  <p><strong>Address class</strong></p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>class Address extends Model
  {
  ...
  
      public function addressable()
      {
          return $this-&gt;morphTo();
      }
  ...
  }
  </code></pre>
  
  <p><strong>Büro class</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class Buro extends Model
  {
  ....
      public function addressable()
      {
          return $this-&gt;morphMany('App\Address', 'addressable');
      }
  ...
  }
  </code></pre>
  
  <p><strong>Order class</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class order extends Model
  {
  ....
      public function addressable()
      {
          return $this-&gt;morphMany('App\Address', 'addressable');
      }
  ...
  }
  </code></pre>
  
  <p>Now I am building a form in the frontend with a SELECT field in order to select just from the Buros that operate in the same State where the order was placed.</p>
  
  <p>Using ELOQUENT I did this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$all_bueros = Buero::where('category_id', $order-&gt;category_id)
                      -&gt;where('title', $order-&gt;client-&gt;addressable[0]-&gt;city-&gt;state-&gt;title) // &lt;- Look at this "title" column name
                      -&gt;pluck('title', 'id')-&gt;all();
  </code></pre>
  
  <p>The problem here is that the <strong>"Title"</strong> column name is  not in <strong>"Buros"</strong> table but three tables away. Exactly in: <strong>buros.addresses.city.state.title</strong></p>
  
  <p>I have tried these syntaxes but no one worked:</p>
  
  <pre class="lang-php prettyprint-override"><code>-&gt;where('buros.addresses.city.state.title', $order-&gt;client-&gt;addressable[0]-&gt;city-&gt;state-&gt;title)   // as object notation
  </code></pre>
  
  <p>and</p>
  
  <pre class="lang-php prettyprint-override"><code>-&gt;where('buros-&gt;addressable[0]-&gt;cities-&gt;state-&gt;title', $order-&gt;client-&gt;addressable[0]-&gt;city-&gt;state-&gt;title)  // using the relation
  </code></pre>
  
  <p>I used the query builder but there is even more messy:</p>
  
  <pre class="lang-php prettyprint-override"><code>$buros = DB::table('buros')
                      -&gt;join('categories', 'categories.id', '=', $order-&gt;category_id)
                      -&gt;join('addresses', 'addresses.addressable_id', '=', $order-&gt;id, 'AND','addresses', 'addresses.addressable_type', '=', 'buros') //two points to join the table
                      -&gt;join('cities', 'addresses.city_id', '=', 'cities.id')
                      -&gt;join('states', 'cities.states', '=', 'states.id')
                      -&gt;select('buros.title', 'buros.id')
                      -&gt;where('states.title', '=', $order-&gt;client-&gt;addressable[0]-&gt;city-&gt;states-&gt;title)
                      -&gt;get();
  </code></pre>
  
  <p>My problem is how to join a table in two points since there is a polymorphic relation. Because I need to join the Address Table in the "Buro-column" (addressable_type) and in the Buro-id column (addressable_id) </p>
  
  <p>Any help will be appretiate</p>