Sam Teng Wong
Sam Teng Wong

Reputation: 2439

Laravel Eloquent groupby and sum

I'm really new to eloquent, and I'm trying groupby the employee_id and do a sum function on the deductions field.. but I'm really having a hard time trying to sum the deductions.

here's my code:

$payroll = Timelog::groupBy('employee_id')->with('employee')->get(['employee_id','deductions']);

what I did is to add sum function after the groupby but I'm having an error..

any help will be appreciated.

Upvotes: 1

Views: 129

Answers (1)

user10186369
user10186369

Reputation:

You should try this:

$payroll = Timelog::select('employee_id',DB::raw('SUM(deductions) as total_deductions'))->with('employee')->groupBy('employee_id')->get();

Upvotes: 3

Related Questions