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

joshuadempseygb's avatar

Best place to calculate model statistics?

I have a model: Shift. A shift represents a shift that a worker performs.

The shift has many columns such as date, scheduled start/end, actual start/end and comments.

About half of these columns are filled in with input from the user. The other half are "calculated" in the store method.

For instance comments and date are taken straight from user input and inserted, and columns such as "duration", "isEmployeeLate", "overtime" etc are calculated from other fields and then inserted.

My question is: where is best to calculate this? It feels kind of icky doing it all in the store method of the resource, but I'm not sure if there's a better solution? Any input would be appreciated.

0 likes
3 replies
LaryAI's avatar
Level 58

One possible solution is to use Laravel's Mutators and Accessors. Mutators allow you to automatically calculate and set values for attributes before saving them to the database, while Accessors allow you to calculate and retrieve values from attributes when accessing them.

In this case, you can define mutators for the calculated attributes in your Shift model. For example, if you have a "duration" attribute that needs to be calculated based on the start and end times, you can define a mutator like this:

public function setDurationAttribute($value)
{
    // Calculate the duration based on the start and end times
    $start = $this->attributes['start_time'];
    $end = $this->attributes['end_time'];
    $duration = // calculate the duration here

    $this->attributes['duration'] = $duration;
}

Similarly, you can define mutators for other calculated attributes like "isEmployeeLate" and "overtime". These mutators will be automatically called when you set the values of the corresponding attributes.

To retrieve the calculated attributes, you can define accessors in your model. For example, if you want to retrieve the "duration" attribute in a formatted way, you can define an accessor like this:

public function getDurationAttribute($value)
{
    // Format the duration here
    $duration = $this->attributes['duration'];
    $formattedDuration = // format the duration here

    return $formattedDuration;
}

With mutators and accessors, you can keep the calculation logic separate from the store method and have it automatically applied whenever you set or retrieve the attributes. This helps to keep your code more organized and maintainable.

Remember to adjust the attribute names and calculation logic according to your specific requirements.

DhPandya's avatar
DhPandya
Best Answer
Level 12

@joshuadempseygb Try using Laravel observers. There you've full control for when and where you need to calculate.

joshuadempseygb's avatar

@DhPandya thank you, this is exactly what I've done now šŸ˜€ a lot more elegant. I've put it in a 'updating' method so the code runs when creating or editing the model, which is a lot better then having the same code in store/update methods šŸ˜„

1 like

Please or to participate in this conversation.