我正在尝试重新设计我的Laravel 4.2代码,并希望在最后几天将结果分组。Laravel groupBy
代码:
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)
- 结果1(最多投票,评论,...)
- 结果2
- 结果3
昨天(日期xx.xx.xx)
- 结果4(最多投票,评论,...)
- 结果5
- 结果6
这可能吗?
使用碳,请参阅下面的答案。 – 2014-12-18 09:15:10