mercredi 26 août 2020

Is there a way to take the number between two dates and subtract with it in Laravel

So I've been trying to find a way to get/take the number in between two dates in fields "from" and "to"(leave table) then take that number(ex. it's 7) and subtract it with another number from the user table a from a field called leaveBalance it has a default number of 20 so I want that ( 20 -7) and the result to be saved in that specific user who requested the leave, in that leaveBalance field after that is changed, also would it be possible to add an if statement to check if the number between dates is bigger than the number allowed that we have on the leaveBalance to just return an error message

This is the leave table

  1. id
  2. user_id
  3. from
  4. to
  5. type
  6. description
  7. status
  8. message

The user table has the leaveBalance field and the two tables don't have a foreign key relation the user_id on the leave only stores the id of that authenticated user when a leave is created and then it only displays the leaves of that id created on the user's view

This is the Leave Controller

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

    }

This is the Leave Model:

{
    use Notifiable;

    protected $guarded=[];
    
    public function user(){
        return $this->belongsTo(User::class,'user_id','id');   
     }
}

This is the view of the Leave

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

I'm open to using carbon in this I don't really know much on carbon but I am aware that it's used for dates and such but since I use date picker is that possible?



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

Aucun commentaire:

Enregistrer un commentaire