【发布时间】:2015-03-25 05:13:37
【问题描述】:
我有 3 张桌子:
Posts
--id
--post
Points
--id
--user_id
--post_id
--points
User(disregard)
--id
--username
我的模型是这样的。
Class Posts extends Eloquent {
function points(){
return $this->hasMany('points', 'post_id');
}
}
Class Points extends Eloquent {
function posts() {
return $this->belongsTo('posts', 'post_id');
}
如何排序,以便返回结果按最高点总和排序。我还需要知道如何获得每个帖子的总点数。
Post_id | Post | Points<-- SumPoints
5 |Post1 | 100
3 |Post2 | 51
1 |Post3 | 44
4 |Post4 | 32
这是我的代码:
$homePosts = $posts->with("filters")
->with(array("points" => function($query) {
$query->select()->sum("points");
}))->groupBy('id')
->orderByRaw('SUM(points) DESC')
->paginate(8);
我可以知道如何通过使用查询构建器和/或模型关系来解决它
【问题讨论】:
-
您发布的代码的结果是什么?你想要一个带有模型/关系或原始查询的 ORM 解决方案吗?