samarsamy92
samarsamy92

Reputation: 113

How Can I Add a Sum with GROUPBY in Laravel 5.6?

I want to make a query in Laravel using ORM and return the result in JSON.

SELECT * FROM `reviews` 
     GROUP BY `school_fk` HAVING (SUM(re_count) > 4)

I tried more ways but it didn't work for me although I run it in MySQL. This was the last trial for me:

Reviews::groupBy('school_fk')->havingRaw('sum(re_count)>', 2)->get();

error : Argument 2 passed to Illuminate\Database\Query\Builder::havingRaw() must be of the type array, integer given

Can anyone help?

reviews table

Upvotes: 0

Views: 44

Answers (1)

Marwelln
Marwelln

Reputation: 29413

Put all your having part inside the first parameter of the function. As of now you're splitting it into two parts.

Reviews::groupBy('school_fk')->havingRaw('sum(re_count) > 2')->get();

Upvotes: 1

Related Questions