简体   繁体   English

获取两个日期的差值并从表 laravel HR 中减去它

[英]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所以我想弄清楚如何使这项工作我有一个休假申请(目前只是一个存储数据的表格),它应该根据日期删除“授予的天数”,如年假或病假从他们例如“从 27/08/2020”和“到 30/08/2020”这是 3 天我想从日期之间和用户表中的 leave_days_granted 字段(始终具有默认值)获取这些数字20) 将它从那里移除,所以 leave_days_granted - 3 = 17 类似这样的东西,所以我可以在视图上显示该用户还剩多少

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

    }

I understand that you have one user with 20 days availables for vacations or permissions.我了解您有一位用户有 20 天的假期或权限可用。

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转换对象 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/您可以在以下位置了解更多使用 Carbon 库: https : //carbon.nesbot.com/docs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM