You can do it, in the model side or controller side, for example
Eloquent query builder:
$entries = TimesheetEntry::with('project', 'client', 'timesheetTask')
->whereHas('project', function ($q) use ($client_id) {
$q->where('client_id', '=', $client_id);
})
->whereBetween('updated_at', [$start, $end])
->selectRaw('client, project, task, sum(hours) as task_hours')
->groupBy('client', 'project', 'task')
->get();
Controller
$entries = TimesheetEntry::with('project', 'client', 'timesheetTask')
->whereHas('project', function ($q) use ($client_id) {
$q->where('client_id', '=', $client_id);
})
->whereBetween('updated_at', [$start, $end])
->get();
$clientProjectTasks = $entries->groupBy(['client', 'project', 'task']);
And then when you need to show a table with the total hours you can do this.
foreach($clientProjectTasks as $clientProjectTask){
$clientProjectTask->sum('hours);
}
Regards