title: Using Model Relationship in a Single Query
body: <p>Consider the following:</p>
  
  <pre><code>$posts = $this-&gt;model-&gt;newQuery()
      -&gt;whereIn('user_id', $user-&gt;following) // specifically this line
      -&gt;orWhere('user_id', $user-&gt;id)
      -&gt;get();
  </code></pre>
  
  <p>The problem with the above is that there are two queries:</p>
  
  <ul>
  <li>Get following: <code>$user-&gt;following</code></li>
  <li>Get posts: Above</li>
  </ul>
  
  <p>This would be much more efficient with the use of a subquery, however, I cannot actually remember the correct way to do it...</p>
  
  <p>I have tried all of the following:</p>
  
  <pre><code>// This was a long-shot...
  ...-&gt;whereIn('user_id', function ($query) use ($user) {
      $query-&gt;raw($user-&gt;following()-&gt;toSql());
  });
  
  // This works but pretty sure it can be done better with eloquent...
  ...-&gt;whereIn('user_id', function ($query) use ($user) {
      $query-&gt;select('follow_id')
          -&gt;from('user_follows')
          -&gt;where('user_id', $user-&gt;id);
  });
  </code></pre>
  
  <p>Is there a way that this can be achieved by using the previously defined relationship <code>$user-&gt;following()</code> instead of manually defining the relationship query like the last example above?</p>
  
  <h1>Reference</h1>
  
  <p>The <code>following</code> relationship is defined as follows:</p>
  
  <pre><code>/**
   * Get the users that the user follows.
   */
  public function following()
  {
      return $this-&gt;belongsToMany('SomeApp\User\Models\User', 'user_follows', 'user_id', 'follow_id')
          -&gt;withTimestamps();
  }
  </code></pre>