title: How to write a inner join in Eloquent ORM in laravel?
body: <p>I have a tables named <code>buildings</code> and <code>flats</code></p>
  
  <p><strong>buildings table</strong></p>
  
  <pre><code>Building_Id | Building_Name | .......| Building_Owned_By | ....
  </code></pre>
  
  <p><strong>flats table</strong></p>
  
  <pre><code>Flat_Id | Flat_Name | ........| Fk_Building_Id | .....
  </code></pre>
  
  <p>and in My Models</p>
  
  <p><strong>Building</strong></p>
  
  <pre><code>class Building extends Eloquent {
      protected $primaryKey = "Building_Id";
      protected $table = 'buildings';
      .......
      .......
      public function flat()
      {
          return  $this-&gt;hasMany('Flat', 'Fk_Building_Id', 'Building_Id');
      }
  }
  </code></pre>
  
  <p><strong>Flat</strong></p>
  
  <pre><code>class Flat extends Eloquent {
      protected $primaryKey = "Flat_Id";
      protected $table = 'flats';
      .......
      .......
      public function building() {
          return $this-&gt;belongsTo('Building','Fk_Building_Id', 'Building_Id');
      }
  }
  </code></pre>
  
  <p>and in my controller</p>
  
  <pre><code>$flats =  Flat::where('Fk_Building_Id', '=',$buildingid)
                 -&gt;where('Building_Owned_By', '=',Auth::user()-&gt;Login_Id)
                 -&gt;orderBy('Flat_Name')
                 -&gt;get(array('Flat_Id as flatId', 'Flat_Name as flatName'))
                 -&gt;toArray();
  </code></pre>
  
  <p>But it returns nothing.</p>
  
  <p>How can we perform inner joins in Eloquent Orm (I dont want to use fluent query)?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks for @Creator for his valuable time and help. He helps me  a lot for finding this. The solution is we have to  use <a href="http://laravel.com/docs/eloquent#dynamic-properties" rel="noreferrer">whereHas</a> and we rewrite the code as </p>
  
  <pre><code>$flats =  Flat::whereHas('building', function($q){ 
                        $q-&gt;where('Building_Owned_By', '=',Auth::user()-&gt;Login_Id);
                 })
                 -&gt;where('Fk_Building_Id', '=',$buildingid)
                 -&gt;orderBy('Flat_Name')
                 -&gt;get(array('Flat_Id as flatId', 'Flat_Name as flatName'))
                 -&gt;toArray();
  </code></pre>