我想獲得之前創建的評論總數的帖子2022-05-12 23:59:59。我還附加了時區過濾器。我嘗試了以下方法:
Route::get('/', function () {
$base = Post::withCount([
'comment' => function ($query) {
$query->select(
'id',
'post_id',
DB::raw('convert_tz(created_at, "UTC", "US/Eastern") as created_at_tz')
)->having('created_at_tz', '<=', '2022-05-12 23:59:59')->count();
},
]);
return $base->get();
});
我得到錯誤:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.id' in 'where clause'
select
count(*) as aggregate
from
(
select
convert_tz(created_at, "UTC", "US/Eastern") as created_at_tz
from
`comments`
where
`posts`.`id` = `comments`.`post_id`
having
`created_at_tz` <= 2022 -05 -12 23: 59: 59
) as `temp_table`
我究竟做錯了什么?
uj5u.com熱心網友回復:
...
->withCount([
'comment' => function ($query) {
$query->whereRaw('convert_tz(created_at, "UTC", "US/Eastern") <= '2022-05-12 23:59:59'));
}])
...
試試這個并更新我的回復
我相信最后的計數是不必要的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485539.html
上一篇:如何使獲取的結果適合模型?
