To calculate the number of participants for all classes within a specified date range multiplied by the duration of each class, you can use the following method in your TrainingController:
use App\Models\Training;
public function calculateReportStaffHours($range_startdt, $range_enddt)
{
$totalHours = Training::whereBetween('startdt', [$range_startdt, $range_enddt])
->orWhereBetween('enddt', [$range_startdt, $range_enddt])
->sum('duration');
$totalParticipants = Training::whereBetween('startdt', [$range_startdt, $range_enddt])
->orWhereBetween('enddt', [$range_startdt, $range_enddt])
->withCount('participants')
->sum('participants_count');
$reportStaffHours = $totalHours * $totalParticipants;
return $reportStaffHours;
}
This method uses the whereBetween method to filter the trainings based on the specified date range. It then calculates the total duration of all trainings within the range using the sum method.
Next, it calculates the total number of participants for all trainings within the range using the withCount method to count the number of participants for each training, and then sums up the counts using the sum method.
Finally, it multiplies the total hours by the total participants to get the reportStaffHours value, which is returned by the method.
You can call this method in your controller action and pass the $range_startdt and $range_enddt variables to get the calculated result:
public function yourControllerAction()
{
$range_startdt = '2022-01-01';
$range_enddt = '2022-01-31';
$reportStaffHours = $this->calculateReportStaffHours($range_startdt, $range_enddt);
// Use the $reportStaffHours value as needed
}
Make sure to adjust the date format and variable names according to your needs.