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.