Laravel Eloquent/DB: Multiple sum fields in grouped select

Viewed 28

Is there any possibility to do the following simple sql-select in Laravel?

SELECT a,b,c,d, SUM(e) as e, SUM(f) as f, SUM(g) as g FROM my_table GROUP BY a,b,c,d

1 Answers

You could use the DB class for that:

DB::table('my_table')
    ->select('a', 'b', 'c', 'd', DB::raw('SUM(e) as e'), DB::raw('SUM(f) as f'), DB::raw('SUM(g) as g'))
    ->groupBy('a', 'b', 'c', 'd')
    ->get()

Documentation

Related