John Davidson

php - Deducting the total leave when the leave request is approved

0 comments
Message:


I have column total leave in table user and column days taken and status in table leave. I tried to deduct the total leave with the leave days taken when the leave is approved but it is not updated. I've tried with this code.


This function is for the applicant to apply for leave.


public function store(Request $request){

$dateFrom = Carbon::createFromFormat('Y-m-d', $request->date_from);
$dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to);
$days_taken = $dateFrom->diffInDays($dateTo)+1;

if($request->leave_id == 1){
if($days_taken > auth()->user()->total_annual){
return redirect()->route('admin.leaverequest.create')->with('error', 'Leave exceed');
}
}
else{
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}

Application::create([
'user_id'=>auth()->user()->id,
'leave_id'=>$request->leave_id,
'date_from'=>$request->date_from,
'date_to'=>$request->date_to,
'days_taken'=> $dateFrom->diffInDays($dateTo)+1,
]);
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}

This is the function for approve/rejecting the leave request.


 public function approval(Request $request, $id){

$leaverequest = Application::find($id);

if($leaverequest->status = $request->approve){

$leaverequest->save();
return redirect()->back();
}
elseif($leaverequest->status = $request->reject){
$leaverequest->save();
return redirect()->back();
}

}

This function is for the calculation to deduct the total leave everytime if the leave request is approved. If not, the total leave will stay as it is.


    public function leaveBalance(Request $request, $id){

$leaverequest = Application::all();
$days_taken = $request->days_taken;
$annual_balance = User::find($id);

if($request->status == 1){
$annual_balance->total_annual = $annual_balance->total_annual - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}

}

The other functions seems to function well except for the leaveBalance function. I've tried using this code but also didn't work.


auth()->user()->total_annual = auth()->user(->total_annual - $days_taken;
auth()->user()->save();


My Answer:
>date_from);
$dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to);
$daysTaken = $dateTo->diffInDays($dateFrom) + 1;

$leave = new Leave();
$leave->user_id = Auth::user()->id;
$leave->date_from = $request->date_from;
$leave->date_to = $request->date_to;
$leave->days_taken = $daysTaken;
$leave->status = 'pending';
$leave->save();

$user = User::find(Auth::user()->id);
$user->total_leave -= $daysTaken;
$user->save();

return redirect()->route('leave.index')->with('success', 'Leave request submitted successfully.');
}

This function deducts the total leave when a leave request is submitted and updates the total leave in the user table. Make sure that the user_id in the leave table matches the id of the user in the user table. Also, ensure that the total_leave column in the user table is set to a numeric data type.

Rate this post

4 of 5 based on 6577 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us