Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

Calculating Class Duration in hours * Number of Participants

Participants * hours

In a Laravel project, I have a “Training” model that stores training class information. Here are the fields:
```
id title startdt
enddt
duration
totaltime shiftcode
shift
stationcode station
unitcode
unit
location
catid
methodid description created_at updated_at

There is a related pivot table name “training_participants” that matches the participants to a training record.  Here are the fields in the training record.
```
training_id 
user_id 
notes 
created_at 
updated_at 

My Training Model looks like this:

```
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia;

class Training extends Model implements HasMedia {

use HasFactory;
use InteractsWithMedia;

// Overwrite the Default Name of the database table
protected $table = 'training';


protected $fillable = [
    'user_id',
    'title',

    'startdt',
    'enddt',
    'duration',
    'totaltime',
    'shiftcode',
    'shift',
    'stationcode',
    'station',
    'unitcode',
    'unit',
    'location',
    'catid',
    'methodid',
    'skills',
    'courseid_internal',
    'courseid_state',
    'courseid_national',
    'location_type',
    'addr_number',
    'addr_street',
    'addr_city',
    'addr_county',
    'addr_state',
    'addr_postalcode',
    'addr_lat',
    'addr_lng',
    'description'
];


public function user()
{
    return $this->belongsTo(User::class);
}

public function category()
{
    return $this->belongsTo(Category::class, 'catid', 'id');
}

public function trainingMethod()
{
    return $this->belongsTo(Category::class, 'methodid', 'id');
}

public function participants()
{
    return $this->belongsToMany(User::class, 'training_participants');
}

public function participantsCount()
{
    return $this->belongsToMany(User::class, 'training_participants')
        ->selectRaw('count(*) as count')
        ->groupBy('training_id');
}

public function instructors()
{
    return $this->belongsToMany(User::class, 'training_instructors');
}

}


```

I need help developing a method/function for my TrainingController that calculates the number of participants for all classes within a specified date range multiplied by the Duration of each class with the same date range. The date range can be accessed by the $range_startdt and $range_enddt variables in the function. The calculated number of hours should be returned as $ReportStaffHours .

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

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.

Please or to participate in this conversation.