So use a withSum
$query = $this->employee->with('punches', 'user')
->where('supervisor_id', '=', 7)
->withSum('punches', 'total_time_converted as total'))
->orderBy('employee_num', $request->order);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I've successfully imported all time clock punches of our employees. I'm trying to get a list of the punches grouped under the employee. I want to total the column 'total_time_converted' before sending it to the view.
I tried these two queries and had no luck.
Error for this one: "call to undefined method Illuminate\Database\Eloquent\Builder::mapInto()"
$query = $this->employee->with('punches', 'user')
->where('supervisor_id', '=', 7)
->whereHas('punches', function (\Illuminate\Database\Eloquent\Builder $query) {
return $query->select('employee_punches.*', DB::raw('SUM(employee_punches.total_time_converted) as total'));
})
->orderBy('employee_num', $request->order);
This one gives me the error "Syntax error or violation: 1055 'laravel.employees.id' isn't in GROUP BY
$query = $this->employee->with('punches', 'user')
->where('supervisor_id', '=', 7)
->whereHas('punches')
->orderBy('employee_num', $request->order)->groupBy('employee_num');
return PunchResource::collection($query);
@ajsmith_codes Try and limit to only have employees withn punches
$data = $this->employee->with('punches', 'user')
->where('supervisor_id', '=', 7)
->has('punches')
->withSum('punches as total', 'total_time_converted'))
->orderBy('employee_num', $request->order)
->get();
Please or to participate in this conversation.