title: Laravel Eloquent join three table-has many through
body: <p>Hi I have three tables</p>
  
  <p><strong>organisation</strong></p>
  
  <pre><code> id | name
   1  | org1
   2  | org2
  </code></pre>
  
  <p><strong>site</strong></p>
  
  <pre><code>id | name  | organisation_id
  1  | site1 | 1
  2  | site2 | 1
  3  | site3 | 2
  </code></pre>
  
  <p><strong>service</strong></p>
  
  <pre><code>id | name     | site_id
  1  | service1 | 1
  2  | service2 | 1
  3  | service2 | 2
  </code></pre>
  
  <p>Here I want to get all the services of organisation (let say 1).
  I can get all the sites of organisation like this but how do I get services:</p>
  
  <pre class="lang-php prettyprint-override"><code>$sites = Site::whereHas('organisation', function ($query) {
      $query-&gt;where('id', 1);
  })-&gt;get();
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <p><strong>organisationModel</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function sites() {
      return $this-&gt;hasMany('App\Site');
  }
  </code></pre>
  
  <p><strong>siteModel</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function organisation(){
      return $this-&gt;belongsTo('App\Organisation');
  }
  
  public function services() {
       return $this-&gt;hasMany('App\Service');
  } 
  </code></pre>
  
  <p><strong>serviceModel</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function site(){
       return $this-&gt;belongsTo('App\Site');
  }
  </code></pre>