demontib87
demontib87

Reputation: 3

Summing over multiple fields in Laravel 4

I'm wondering if it is possible to take the sum of multiple fields in one query using the fluent query builder.

For exemple :

SELECT sum(nb_visit + nb_recommendation)
FROM Stats 
GROUP BY id

So far is what i have :

Stats::sum('nb_visit+nb_recommendation');

Upvotes: 0

Views: 47

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87789

You can use QueryBuilder to do this:

DB::table('stats')
    ->select(DB::raw('sum(nb_visit + nb_recommendation)'))
    ->groupBy('id')
    ->get();

Upvotes: 1

Related Questions