Vit
Vit

Reputation: 154

Get difference number of two dates and subtract it from table laravel HR

So I'm trying to figure out how to make this work I have a Leave Application(which is currently just a form that stores the data) which is supposed to remove the "Days Granted" like Annual Leaves or Sick Leaves based on the dates from them for example "from 27/08/2020" and "to 30/08/2020" which is 3 days I want to get those numbers from between the dates and in the user table on the leave_days_granted field(which always has default value 20) to remove it from there so leave_days_granted - 3 = 17 something like this so I can display it on the view how many that user has left

I have added this in the user model

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);
    }

}

And this is added in the Leave Model

class Leave
{
    protected $dates = ['from', 'to'];

    public function numberOfDays()
    {
        return $this->from->diffInDays($to);
    }
}

So here I don't get why this isn't working it's not changing anything in the database when I request a leave and I'm not sure now if I'm supposed to add something more on the Leave controller to call this or the View of the Leave

This is how I get the dates on the view

<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="">

This is the form link This is the users table link This is the leaves table link

This is the LeaveController

 public function index()
    {

        if(Auth::guard('admin')->check())
        {
            $users = User::all();
            $leaves = Leave::latest()->paginate(5);
            return view('admin/leave/index', compact('leaves'),compact('users'));
        }

        $role = Auth::guard('web')->user()->role_id;
        if($role == 1)
        {
            return view('unauthorized');
        }
        else
        {
            $leaves = Leave::latest()->paginate(5);
            return view('admin/leave/index', compact('leaves'));
        }
    
        
    }
    public function create()
     {
        $leaves = Leave::latest()->where('user_id',auth()->user()->id)->paginate(5);
        return view('leave.create',compact('leaves'));
    }
public function store(Request $request)
    {
        $this->validate($request,[
            'from'=>'required',
            'to'=>'required',
            'description'=>'required', 
            'type'=>'required'
            ]);
            $data=$request->all();
            $data['user_id']=auth()->user()->id;
            $data['message']='';
            $data['status']=0;
            $leave =Leave::create($data);
            
            $admins = Admin::all();
            $users = User::where('role_id', 2)->get();

            foreach ($admins as $admins) {
                foreach($users as $users){
                $admins->notify(new LeaveSent($leave));
                $users->notify((new LeaveSent($leave)));
            }
        }
        return redirect()->back()->with('message','Leave Created');

    }

Upvotes: 0

Views: 251

Answers (1)

Daniel Lara
Daniel Lara

Reputation: 36

I understand that you have one user with 20 days availables for vacations or permissions.

You need that for each leave request, this count days of leave, and deduct of days availables for each user

I make this:

  1. transform dates in object Carbon

  2. get diff days

  3. update days availables for user

  4. In your controller

     //transform dates in object Carbon
     $from = Carbon::parse($request->from);
     $to = Carbon::parse($request->to);
     //get diff days
     $diff = $from->diffInDays($to)
     //find user and deduct days
     $user = User::findOrFail(Auth::user()->id);
     $user->days_availables = $user->days_availables - $diff
     $user->update();
    

you learn more use Carbon library in: https://carbon.nesbot.com/docs/

Upvotes: 1

Related Questions