jeudi 27 août 2020

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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2YEBWYx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire