calculate Attendance

Hello, I would like to implement the number of hours worked into the formula via Carbon. Can I ask for advice?
24 Replies
Tieme
Tieme10mo ago
Yes you can ask advice, dont know if anyone can help you but please ask. Also share some code so whe know what you are talking about.
BlackShadow
BlackShadow10mo ago
I think you can make mutator for this?
Tieme
Tieme10mo ago
i have something simular, if you want to set you total you could do something like this in your model
public static function boot()
{
parent::boot();
static::updating(function ($model) {
if ($model->{'from'} != null and $model->{'to'} != null) {
$Minutes = $model->{'from'}->diffInMinutes($model->{'to'});
$model->{'total'} = $model->{'total'} + $Minutes;
}
});
}
public static function boot()
{
parent::boot();
static::updating(function ($model) {
if ($model->{'from'} != null and $model->{'to'} != null) {
$Minutes = $model->{'from'}->diffInMinutes($model->{'to'});
$model->{'total'} = $model->{'total'} + $Minutes;
}
});
}
For me i can start and stop time so it is different than yours, i store the time in minutes and have a function to format it as hours.
public function getHoursAttribute()
{
$minuten = $this->total;
$uren = floor($minuten / 60); // The number of complete hours
$resterendeMinuten = $minuten % 60; // The remaining minutes

return sprintf('%02d:%02d', $uren, $resterendeMinuten);
}
public function getHoursAttribute()
{
$minuten = $this->total;
$uren = floor($minuten / 60); // The number of complete hours
$resterendeMinuten = $minuten % 60; // The remaining minutes

return sprintf('%02d:%02d', $uren, $resterendeMinuten);
}
marty6236
marty6236OP10mo ago
I don't know what you mean.
BlackShadow
BlackShadow10mo ago
^ If you just use 2 dateTime you can use diffInHours 😛
Tieme
Tieme10mo ago
Will this also show the remaing minutes?
BlackShadow
BlackShadow10mo ago
You could by using $end->diffInMinutes(now()) Something like that (not tested)
Tieme
Tieme10mo ago
For me i only have the minutes total, i can start and stop and than later start and stop again and it will add minutes. did not yet get this working with carbon or something like that
Tieme
Tieme10mo ago
Sorry, i know now why i use minutes. i want total hours, and if the hours are more dan 24 it showd me 2 insted of 26.
BlackShadow
BlackShadow10mo ago
$start->diffInHours($end)
Tieme
Tieme10mo ago
true, only than for every time entry i need to group them. Now i have 1 time entry, my customers dont want to know what time i started and what time i ended only the total time for me to work on a project line
BlackShadow
BlackShadow10mo ago
I don't understand:squint: , you can probably create an observer that calculates the time when you create the record, or you can make an attribute.
Tieme
Tieme10mo ago
I'm rebuilding my project as saas application and will try this when i'm at this point in coding.
marty6236
marty6236OP10mo ago
I added the sample code and it threw me an error Calling member function diffInMinutes() on string
BlackShadow
BlackShadow10mo ago
Yes that only works on a Carbon object
Tieme
Tieme10mo ago
You need to cast in your model something like this
protected $casts = [
'date' => 'date',
'from' => 'datetime',
'to' => 'datetime'
];
protected $casts = [
'date' => 'date',
'from' => 'datetime',
'to' => 'datetime'
];
marty6236
marty6236OP10mo ago
it seems to work but the data is not uploaded to the database
Tieme
Tieme10mo ago
And if you update? this will set total on creating and updating
public static function boot()
{
parent::boot();
static::creating(function ($model) {
if ($model->{'from'} != null and $model->{'to'} != null) {
$Minutes = $model->{'from'}->diffInMinutes($model->{'to'});
$model->{'total'} = $model->{'total'} + $Minutes;
}
});
static::updating(function ($model) {
if ($model->{'from'} != null and $model->{'to'} != null) {
$Minutes = $model->{'from'}->diffInMinutes($model->{'to'});
$model->{'total'} = $model->{'total'} + $Minutes;
}
});
}
public static function boot()
{
parent::boot();
static::creating(function ($model) {
if ($model->{'from'} != null and $model->{'to'} != null) {
$Minutes = $model->{'from'}->diffInMinutes($model->{'to'});
$model->{'total'} = $model->{'total'} + $Minutes;
}
});
static::updating(function ($model) {
if ($model->{'from'} != null and $model->{'to'} != null) {
$Minutes = $model->{'from'}->diffInMinutes($model->{'to'});
$model->{'total'} = $model->{'total'} + $Minutes;
}
});
}
BlackShadow
BlackShadow10mo ago
Would be nice to have observer no?
marty6236
marty6236OP10mo ago
not even with the update
Tieme
Tieme10mo ago
How does you model class look now?
marty6236
marty6236OP10mo ago
it's working now thank you very much
Want results from more Discord servers?
Add your server