I'll just head on to the question directly, I'm currently trying to build an HR(Human Resources) Application on Laravel and at the Leave System where a user can take a specific type of Leave(Vacation and such), I would like to create a Balance system where the user selects a date from and date to and that number in between them gets deducted from the main balance that a user has and that would be the number 20 which I added to my user table on a field called leave_balance(the number is set by default as 20 when a user is created).
Now except deducting the number I would also like to add a function somehow that adds a specific number to the leave_balance every day ex 0.05 I am suspecting I can do this with carbon but I have not the slightest idea how since im relatively new to Laravel
So this is the Leave Table(it is not connected by a foreign key with the user's table):
The user_id is only there to store the id of the authenticated user who creates a leave it is not related by a foreign key
This is what I was thinking of adding in the LeaveController:
// checks for table if employee leave is at 0
$employee = DB::table('leaves')->where('id', $id)->where('leave', 0)->get()->all();
$employeeObj = DB::table('leaves')->where('id', $id)->get()->all();
// init for needed condition
$leaveBool = false;
if(sizeof($employee) < 1) {
$leaveBool = true;
}
// leave is not at 0
if(!$leaveBool) {
DB::table('leaves')->update([
'id' => $id,
'leave' => $employeeObj['leaves'] - 1,
]);
}
I would like to fix this code so that I can add this in the Leave Controller(also if it's possible to explain to me where exactly I should put such a code in the controller) by fix I mean to not just removed -1 but the number between "from" and "to"
I'm not sure if these are helpful but I will post them too just to make it easier to see,
This is the Leave Model:
class Leave extends Model
{
use Notifiable;
protected $guarded=[];
public function user(){
return $this->belongsTo(User::class,'user_id','id');
}
}
This is the view of the Leave
<div class="card-body">
<form method="POST" action="{{route('leaves.store')}}">
@csrf
<div class="form-group">
<label>From Date</label>
<div class="col-md-6">
<input class="datepicker" type="text" class="form-control @error('from') is-invalid @enderror" name="from" required="">
@error('from')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group">
<label>To Date</label>
<div class="col-md-6">
<input class="datepicker1" type="text" class="form-control @error('to') is-invalid @enderror" name="to" required="">
I used datepicker for the date, this would be a visualization of what I'm trying to achieve

Also by any means, if you have a better code or idea on how I can do this do share it your way it may probably be heaps better
What if you make a method leaveBalance() on the User. Since it somewhat of a computed property this might make it easier than keeping the numbers right when creating/deleting/updating Leave objects. To do that add a property leave_days_granted on the User (defaults to 20). Remove leave_balance.
Then you could do something like:
class User
{
public function leaveBalance()
{
$daysUsed = $this->leaves->map->numberOfDays()->sum();
return $this->leave_days_granted - $daysUsed;
}
// You can also add a helper to make your controller more readable.
public function hasAvailableLeave()
{
return $this->leaveBalance() > 0;
}
public function leaves()
{
return $this->hasMany(\App\Leave::class);
}
}
Leave model:
class Leave
{
protected $dates = ['from', 'to'];
public function numberOfDays()
{
return $this->from->diffInDays($to);
}
}
$this->leaves references the relationship method leaves(). Using it without () will automatically retrieve the related items.
->map->numberOfDays() is shorthand for
->map(function($leave) {
return $leave->numberOfDays();
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With