public function applyForLeave() { $all = Input::all(); $mgr = $this->getManager(); $authUserId = $this->getUserId(); $all['mgr_id'] = $mgr['id']; $all['user_id'] = $authUserId; $all['status'] = 'Eequest'; //TODO //1) check if user exceed the no of days entitled for leaves fer year. $entitle = Entitlement::where('id', '=', $all['entitlement_id'])->first(); /* $leaves = LeaveRequest::where('user_id', '=', $authUserId) ->where('entitlement_id', '=', $all['entitlement_id'])->get(); */ $noOfDays = $entitle['no_of_days']; $leaveSum = DB::table('leave_requests')->where('user_id', '=', $authUserId)->where('entitlement_id', '=', $all['entitlement_id'])->sum('no_of_days'); $totalLeaves = $leaveSum + $all['no_of_days']; if ($totalLeaves > $noOfDays) { return Response::jsend('fail', array('msg' => sprintf("You are Exceeding the Limit of %s", $entitle['name']))); } //2) if the user is already applied to that leave //3) create Leave request. LeaveRequest::create($all); return Response::jsend('success', array('msg' => 'Leave request is submitted.')); }