問題描述
我正在嘗試使用這段代碼對 laravel 的 eloquent 查詢構建器的 withCount 方法執行 where 子句.
I am trying to do a where clause on withCount method of laravel's eloquent query builder using this piece of code.
$posts = Post::withCount('upvotes')->where('upvotes_count', '>', 5)->get();
這段代碼給了我這個錯誤.
and this code is giving me this error.
SQLSTATE[42S22]:未找到列:1054 未知列upvotes_count"在where 子句"(SQL:select , (select count() from upvotes
whereupvotes
.upvoteable_id
= posts
.id
和 upvotes
.upvoteable_type
= AppPost) as upvotes_count
from posts
where upvotes_count
> 5)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upvotes_count' in 'where clause' (SQL: select , (select count() from
upvotes
whereupvotes
.upvoteable_id
=posts
.id
andupvotes
.upvoteable_type
= AppPost) asupvotes_count
fromposts
whereupvotes_count
> 5)
所以我可以猜測的是 upvotes_count 沒有被選中,因此沒有找到該列,但是如果我執行這段代碼.
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.
$posts = Post::withCount('upvotes')->get();
然后我得到這個輸出.
{
"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
},
這基本上意味著 upvotes_count 被選中,因此我真的很困惑如何解決這個問題.
Which basically means that upvotes_count is being selected, hence i am really confused about how to solve this problem.
(下面給出了我迄今為止嘗試過的更多選項以及與之相關的相應錯誤.)
(More options that I tried so far are given below with the respective error associated to it.)
$posts = Post::where('id', $id)->withCount(['upvotes' => function($query) {
$query->where('upvotes_count', '>', 5);
}])->get();
錯誤.
SQLSTATE[42S22]: Column not found: 1247 Reference 'upvotes_count' not supported (forward reference in item list) (SQL: select , (select count() from upvotes)code> where
upvotes
.upvoteable_id
= posts
.id
and upvotes
.upvoteable_type
= AppPost and upvotes_count
> 5) as upvotes_count
from posts
where id
=1)
SQLSTATE[42S22]: Column not found: 1247 Reference 'upvotes_count' not supported (forward reference in item list) (SQL: select , (select count() from
upvotes
whereupvotes
.upvoteable_id
=posts
.id
andupvotes
.upvoteable_type
= AppPost andupvotes_count
> 5) asupvotes_count
fromposts
whereid
= 1)
代碼.
$posts = Post::where('id', $id)->with(['upvotes' => function($query) {
$query->select('upvoteable_id AS upvotes_count');
}])->where('upvotes_count', '>', 5)->get();
和
$posts = AppPost::where('id', $id)->with(['upvotes' => function($query) {
$query->selectRaw('upvoteable_id AS upvotes_count');
}])->where('upvotes_count', '>', 5)->get();
錯誤.
SQLSTATE[42S22]:未找到列:1054 Unknown column 'upvotes_count' in 'where clause'(SQL:從 posts
中選擇 * 其中 id
= 1 和 <代碼>upvotes_count > 5)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upvotes_count' in 'where clause' (SQL: select * from
posts
whereid
= 1 andupvotes_count
> 5)
<小時>
我只想在與父模型有關系的 count() 方法上使用 where 子句.
I just want to use where clause on a count() method which is in a relationship with a parent model.
推薦答案
您可以通過使用:
$posts = Post::withCount('upvotes')
->having('upvotes_count', '>', 5)
->get();
這篇關于Laravel 在 withCount 方法上使用 where 子句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!