小编sla*_*bot的帖子

Laravel在withCount方法上使用where子句

我正在尝试使用这段代码在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 ,(selectupvotes where ()from where upvotes.upvoteable_id= posts.idand upvotes.upvoteable_type= App\Post)upvotes_countfrom postswhere where upvotes_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)

php mysql sql laravel eloquent

8
推荐指数
4
解决办法
6800
查看次数

标签 统计

eloquent ×1

laravel ×1

mysql ×1

php ×1

sql ×1