title: 3 models relationship in Laravel 5
body: <p>I have 3 Models.</p>
  
  <ul>
  <li>Project {id, name}</li>
  <li>Platform {id, name}</li>
  <li>Version {id, value}</li>
  </ul>
  
  <p>One Project can have multiple Platforms and each Platform can have multiple Versions <strong>within a Project</strong>. I have the following table which relates the models:</p>
  
  <p><a href="https://i.stack.imgur.com/i1rQo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i1rQo.png" alt="enter image description here"></a></p>
  
  <p>As you can see Project 6 has versions 1 and 3 on platform 1, while Project 8 has versions 1 and 2 on the same platform.</p>
  
  <p>I want to get all Projects' Platforms and versions of those Platforms depending on the Project.</p>
  
  <p>For example, I'd like to do</p>
  
  <pre><code>App\Project::find(6)-&gt;platform()-&gt;with('version')-&gt;get();
  </code></pre>
  
  <p>and get only versions with ids of 1 and 3 on my version collection of platform 1 of this project.</p>
  
  <p>This is what I have in Project Model:</p>
  
  <pre><code>public function platform()
  {
      return $this-&gt;belongsToMany('App\Platform')-&gt;withPivot('version_id')-&gt;distinct();
  }
  </code></pre>
  
  <p>This is what I have in Platform Model:</p>
  
  <pre><code>public function version()
  {
      return $this-&gt;belongsToMany('App\Version', 'platform_project')-&gt;distinct();
  }
  </code></pre>
  
  <p>Could you please help me figure out the best way to achieve the desired result?</p>