Laravel using where clause on a withCount method
I think using has() is the best solution:
Post::has('upvotes','>',5)->withCount('upvotes')->get()
You could also use a filter:
Post::withCount('upvotes')->get()->filter(function($post) { return $post->upvotes_count > 5; })
You could also disable strict mode in config/database.php (probably not a good idea)
'strict' => false,
Post::withCount('upvotes')->having('upvotes_count','>',5)->get()
You could also try to add a groupBy clause (using having in strict mode), but this will likely require you to include every column in your table (due to 'ONLY_FULL_GROUP_BY'), which could break things if you ever add another column to your table, and probably won't work anyway because I think you need to include 'upvotes_count' in the groupBy and it seems to be a non grouping field.
I'm not sure if this was implemented after your question, but you can now do it like this
$posts = Post::has('upvotes','>',5)->get();
another good way to do this we can filter that separately and even assign an alias to that column name
$posts = Post::withCount([
'upvotes',
'upvotes as upvotes_count' => function ($query) {
$query->where('upvotes_count', '>', 5);
}])
->get();
Now in blade you can do
$posts->upvotes_count
You can achieve requested result by using:
$posts = Post::withCount('upvotes')
->having('upvotes_count', '>', 5)
->get();