我正在尝试使用这段代码在laravel的eloquent查询构建器的withCount方法上执行where子句.
$posts = Post::withCount('upvotes')->where('upvotes_count', '>', 5)->get();
Run Code Online (Sandbox Code Playgroud)
这段代码给了我这个错误.
SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'upvotes_count'(SQL:select ,(select
upvoteswhere ()from whereupvotes.upvoteable_id=posts.idandupvotes.upvoteable_type= App\Post)upvotes_countfrompostswhere whereupvotes_count> 5)
所以我可以猜到,没有选择upvotes_count,因此没有找到列,但是如果我执行这段代码.
$posts = Post::withCount('upvotes')->get();
Run Code Online (Sandbox Code Playgroud)
然后我得到这个输出.
{
"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", …Run Code Online (Sandbox Code Playgroud)