I want to get ranking number in Laravel.
DB is here.
JavaScript
x
id Bigint
name string
point BigInt
..
I want to get ranking number in point column. What should I do?
now code is this.
JavaScript
User::where('id', 1)->first();
if I have these datas.
JavaScript
id name score
1 AA 10
2 CD 10
3 ER 40
4 DR 5
I want to get ranking number ex) id 1 => 2 (or3) ex) id 3 => 1
Advertisement
Answer
You can get the rank as below:
JavaScript
User::selectRaw("SELECT id, name, point, FIND_IN_SET( point, (
SELECT GROUP_CONCAT( DISTINCT point ORDER BY point DESC ) FROM
user )
) AS rank
FROM user")
->get()