Group Then Sum? (For leaderboard)

Posted 2 months ago by onebytesh

Hello, I'm trying to group a table by user id, then adding all of the scores together, and then sort it. here's how the table is structured

+----+---------+-------+
| ID | USER_ID | SCORE |
+----+---------+-------+
| 1  | 1       | 100   |
+----+---------+-------+
| 2  | 1       | 50    |
+----+---------+-------+
| 3  | 2       | 100   |
+----+---------+-------+
| 4  | 2       | 40    |
+----+---------+-------+
| 5  | 3       | 100   |
+----+---------+-------+

I Would like for the table to be like this

+---------+-------+
| USER_ID | SCORE |
+---------+-------+
| 1       | 150   |
+---------+-------+
| 2       | 140   |
+---------+-------+
| 3       | 100   |
+---------+-------+

It's for leaderboard system. How can I achieve this with eloquent? Thanks!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.