jeudi 26 janvier 2017

How Can I Get SUM Array On Laravel5?

My postgresql database like this.

SELECT * FROM point_records;

 id | user_id | point |     created_at      |     updated_at
----+---------+-------+---------------------+---------------------
  1 |       1 |   100 | 2017-01-27 09:39:47 | 2017-01-27 09:39:47
  3 |       2 |   100 |                     |
  4 |       2 |   100 |                     |
  5 |       3 |   100 |                     |
  6 |       3 |   100 |                     |
  7 |       3 |   100 |                     |

SELECT user_id, SUM(point) AS points FROM point_records GROUP BY(user_id) ORDER BY points desc;

 user_id | points
---------+--------
       3 |    300
       2 |    200
       1 |    100

I wanna get point ranking to array. So I wish to do like this.

$points = \App\PointRecord:: ...??? -> groupBy('user_id') -> orderBY('points') -> toArray();
$your_score = array_search($user -> id, $points) + 1;

Anyone knows?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire