我正试图重新设计我的Laravel4.2代码,并希望根据过去几天的结果列出一个列表。
代码:
public function getTrending($type = null, $category = null)
{
$posts = $this->posts($type, $category)->with('comments', 'votes', 'category', 'user', 'votes.user')
->leftJoin('votes', 'posts.id', '=', 'votes.post_id')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->select('posts.*', DB::raw('count(comments.post_id)*7 + count(votes.post_id)*30 + posts.views as popular'))
->groupBy('posts.id')->with('user')->orderBy('popular', 'desc')->whereMonth('posts.created_at', '>=', date('n', strtotime('-1 month')))
->paginate(perPage());
return $posts;
}我想分组结果,因为它是(相关性:评论,投票,访问)+分组的结果在一个日常的基础上。
比如:
今天(有日期xx.xx.xx)
昨天(附日期xx.xx.xx)
这个是可能的吗?
发布于 2014-12-18 09:06:30
你可以使用碳;
->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('Y'); // grouping by years
//return Carbon::parse($date->created_at)->format('m'); // grouping by months
});发布于 2014-08-30 09:32:21
不,我不相信你能在这样的一个查询中把你的数据分组。
假设您的代码为您提供了您想要的数据集,而不按日期分组,我将遍历我想要的日期,并获取每天的数据,并将其格式化为日期标题等。
https://stackoverflow.com/questions/25580829
复制相似问题