Laravel3 group_by + sum + paginate?

195 Views Asked by At

I have to write a query like : SELECT azon, SUM(points) points FROM stats GROUP BY azon; Also I need to paginate them in Laravel3.

So I tried Stats::group_by('azon')->sum('points')->paginate(20);. Of course this did not work. So I tried

$stats2 = DB::table('stats')
                ->select('azon', DB::raw('SUM(points) as points'))
                ->group_by('azon')
                ->paginate(20);

No results. The last thing I tried was DB::query('SELECT azon, SUM(points) points FROM test_stats GROUP BY azon') but this returns an array and I cannot paginate an array. Any ideas, workarounds. I hope Laravel3 has something for this as this is a basic query.

1

There are 1 best solutions below

0
serdar.sanri On BEST ANSWER

The issue with your query is, select function in Query Builder accepts array parameter.

$stats2 = DB::table('stats')
    ->select( array( 'azon', DB::raw('SUM(points) as points') ) )
    ->group_by('azon')
    ->paginate(20);