I am exporting an Excel file using Maatwebsite/Laravel-Excel.
I have Projects > Modules (for each Project) > Activities (for each module) > dates > time (for each date). I can display time for each date, and then I am summing up the total hours spent on each activity, but then I have to sum up total hours for all activities under each module. Which I am not able to do so?
Excel data
# Project A #
## Module A date1 date2 date3 total total2
### Activity A 1 2 3 6 18 (this is what I wanna display)
### Activity B 1 2 3 6
### Activity C 1 2 3 6
Blade
@foreach($project as $pro)
@foreach($pro->modules as $mod)
@foreach($activity as $act)
<tr>
<td>{{$pro->name}}</td>
<td>{{$mod->name}}</td>
<td>{{$act->name}}</td>
@foreach($time[$pro->id][$mod->id][$act->id] as $log)
<td>{{$log}}</td>
@endforeach
<td>
{{ array_sum($time[$pro->id][$mod->id][$act->id])}}
{{-- This is where I get the total --}}
</td>
<td>{{-- Here I want to display total2 --}}</td>
</tr>
@endforeach
@endforeach
@endforeach
Laravel comes with a very powerfull tool - collections. You should deffinitely take a look at the awailable methods. Also, awoid using any kind of calculations in your view files and process the data you wish to output before passing it to the view
Sum it on the controller, add a property on the objects called "sum" and then display it on the blade
$activity->setAttribute('sum', 'sumresult');