Php 检索最后一天7篇评论最多的5篇文章
我有带评论的文章。我想在我的主页上发布5个热门项目,根据7天内收到的评论。我试过这个:Php 检索最后一天7篇评论最多的5篇文章,php,laravel,Php,Laravel,我有带评论的文章。我想在我的主页上发布5个热门项目,根据7天内收到的评论。我试过这个: $popularArticles = Article::published() ->whereHas('comments') ->withCount('comments') ->where('created_at', '>', \Carbon\Carbon::now()->subWeek()) ->orderBy('comments_count', 'DESC') ->
$popularArticles = Article::published()
->whereHas('comments')
->withCount('comments')
->where('created_at', '>', \Carbon\Carbon::now()->subWeek())
->orderBy('comments_count', 'DESC')
->take(5)
->get();
但使用这种方法,7天前创建的文章将被“忽略”。我想说的是,过去7天的评论决定了一篇文章是否受欢迎
非常感谢您您需要像这样过滤已计数的评论:
$popularArticles = Article::published()
->has('comments')
->withCount(['comments' => function ($q) {
$q->where('created_at', '>', Carbon\Carbon::now()->subWeek());
}])
->latest('comments_count')
->take(5)
->get();