I have a Leave Application that checks when the users request a leave and after approval, it removes the days from the total amount they have left in the database the problem is the leave is taken only the latest so if 2 people were requesting at the same time it would take it for both the same amount because it's only taking it from the latest which is here in the User Model
public function leaveBalance($id)
{
$leave =new Leave;
$daysUsed= $leave->numberOfDays($id);
$days_granted = User::where('id', $id)
->latest()->first();
$leave_days_granted = $days_granted->leave_days_granted;
return $leave_days_granted - $daysUsed;
}
And here in the Leave Model
protected $dates = ['from', 'to'];
public function numberOfDays($id)
{
$datesx =Leave::where('user_id', $id)
->latest()->first();
$from =$datesx->from;
$to =$datesx->to;
$datetime1 = new DateTime($from);
$datetime2 = new DateTime($to);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');//now do whatever you like with $days
return $days;
}
As you can see it's taking it from the ->latest()->first(); but I don't know another way to select it and thus why I need help I would appreciate any suggestion
User::where('id', $id)->first();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.