Basically I have a point log table as follows:
user_id | points
1 | 10
2 | 20
1 | 30
1 | 4
2 | 6
6 | 8
and I am looking to group the users by their total points and display their ranking.
This is what I have so far in my User.php model:
public function getPointRankAttribute() {
return $this->hasMany('App\PointLog')->select(DB::raw('
SELECT s.*, @rank := @rank + 1 rank FROM (
SELECT user_id, sum(points) TotalPoints FROM t
GROUP BY user_id
) s, (SELECT @rank := 0) init
ORDER BY TotalPoints DESC
')
);
}
and then display in my blade template as follows:
Your point rank: {{ $user->pointRank }}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community