title: Laravel using where clause on a withCount method
body: <p>I am trying to do a where clause on withCount method of laravel's eloquent query builder using this piece of code.</p>
  
  <pre><code>$posts = Post::withCount('upvotes')-&gt;where('upvotes_count', '&gt;', 5)-&gt;get();
  </code></pre>
  
  <p>and this code is giving me this error.</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upvotes_count' in 'where clause' (SQL: select <em>, (select count(</em>) from <code>upvotes</code> where <code>upvotes</code>.<code>upvoteable_id</code> = <code>posts</code>.<code>id</code> and <code>upvotes</code>.<code>upvoteable_type</code> = App\Post) as <code>upvotes_count</code> from <code>posts</code> where <code>upvotes_count</code> > 5)</p>
  </blockquote>
  
  <p>So from what I can guess is that upvotes_count isn't selected and hence the column is not being found, BUT if I execute this piece of code.</p>
  
  <pre><code>$posts = Post::withCount('upvotes')-&gt;get();
  </code></pre>
  
  <p>Then I am getting this output.</p>
  
  <pre><code>{
  "id": 1,
  "user_id": 15,
  "title": "Voluptatum voluptas sint delectus unde amet quis.",
  "created_at": "2016-10-07 13:47:48",
  "updated_at": "2016-10-07 13:47:48",
  "upvotes_count": 7
  },
  {
  "id": 2,
  "user_id": 2,
  "title": "Molestiae in labore qui atque.",
  "created_at": "2016-10-07 13:47:48",
  "updated_at": "2016-10-07 13:47:48",
  "upvotes_count": 2
  },
  </code></pre>
  
  <p>Which basically means that upvotes_count is being selected, hence i am really confused about how to solve this problem.</p>
  
  <p>(More options that I tried so far are given below with the respective error associated to it.)</p>
  
  <pre><code>$posts = Post::where('id', $id)-&gt;withCount(['upvotes' =&gt; function($query) {
          $query-&gt;where('upvotes_count', '&gt;', 5);
      }])-&gt;get();
  </code></pre>
  
  <p>error.</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1247 Reference 'upvotes_count' not supported (forward reference in item list) (SQL: select <em>, (select count(</em>) from <code>upvotes</code> where <code>upvotes</code>.<code>upvoteable_id</code> = <code>posts</code>.<code>id</code> and <code>upvotes</code>.<code>upvoteable_type</code> = App\Post and <code>upvotes_count</code> > 5) as <code>upvotes_count</code> from <code>posts</code> where <code>id</code> = 1)</p>
  </blockquote>
  
  <p>code.</p>
  
  <pre><code>$posts = Post::where('id', $id)-&gt;with(['upvotes' =&gt; function($query) {
          $query-&gt;select('upvoteable_id AS upvotes_count');
      }])-&gt;where('upvotes_count', '&gt;', 5)-&gt;get();
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>$posts = \App\Post::where('id', $id)-&gt;with(['upvotes' =&gt; function($query) {
          $query-&gt;selectRaw('upvoteable_id AS upvotes_count');
      }])-&gt;where('upvotes_count', '&gt;', 5)-&gt;get();
  </code></pre>
  
  <p>error.</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upvotes_count' in 'where clause' (SQL: select * from <code>posts</code> where <code>id</code> = 1 and <code>upvotes_count</code> > 5)</p>
  </blockquote>
  
  <hr>
  
  <p>I just want to use where clause on a count() method which is in a relationship with a parent model.</p>