我有名为 Post 和 PostView 的模型。一个 Post 可以有多个 PostView。我需要获取按过去 30 天内创建的 PostViews 计数排序的分页帖子列表。
我可以这样做withCount:
// works but S L O W
$posts = Post::withCount(['views' => function($query) {
$query->where('post_views.created_at', '>=', 'DATE_ADD(CURDATE(), INTERVAL -30 DAY)');
}])
->orderBy('views_count')
->paginate(10);
但是,这会生成一个非常慢的查询,大约需要 24 秒。
使用原始 sql 我可以更有效地获得正确的结果,如何将其转换为分页模型集合?
这会生成正确的查询来获取前 10 个,但结果集合是空的。我认为这与selectRaw
$posts = Post::selectRaw('posts.*, count(post_views.id) as views_count')
->join('post_views', function($join) {
$join->on('posts.id', '=', 'post_views.post_id')
->where('post_views.created_at', '>=', 'DATE_ADD(CURDATE(), INTERVAL -30 DAY)');
})
->groupBy('posts.id')
->orderBy('views_count', 'DESC')
->take(10)
->get();
如果我运行直接在 mysql 中生成的查询,我会得到结果:(注意 - 为简洁起见,截断为 posts.id)
mysql> select posts.id, count(post_views.id) as views_count from `posts` inner join `post_views` on `posts`.`id` = `post_views`.`post_id` and `post_views`.`created_at` >= DATE_ADD(CURDATE(), INTERVAL -30 DAY) group by `posts`.`id` order by `views_count` desc limit 10;
+--------+-------------+
| id | views_count |
+--------+-------------+
| 150277 | 22 |
| 43843 | 6 |
| 138789 | 4 |
| 180565 | 4 |
| 50555 | 3 |
| 2679 | 3 |
| 188572 | 3 |
| 217454 | 3 |
| 136736 | 3 |
| 126472 | 2 |
+--------+-------------+
10 rows in set (1.26 sec)
任何帮助表示赞赏,谢谢。
qq_笑_17
慕的地6264312
杨魅力
HUX布斯
随时随地看视频慕课网APP