/** * Get the upcoming leave for company (getter) */ public function getLeaveUpcomingDatesAttribute() { $string = ''; $leave = CompanyLeave::where('to', '>=', Carbon::today()->toDateTimeString())->where('company_id', $this->id)->orderBy('from')->get(); foreach ($leave as $l) { if ($l->from == $l->to) { $string .= $l->from->format('d/m') . ', '; } else { $string .= $l->from->format('d/m') . ' - ' . $l->to->format('d/m') . ', '; } } return rtrim($string, ', '); }
private function getCompanyLeave() { $company_list = Auth::user()->company->companyList()->pluck('id')->toArray(); $leave_records = CompanyLeave::where('to', '>=', Carbon::today()->toDateTimeString())->whereIn('company_id', $company_list)->orderBy('from')->get(); $company_leave = []; foreach ($leave_records as $leave) { $company = Company::find($leave->company_id); $array = ['summary' => $company->leave_upcoming_dates]; // Loop through leave 'from' -> 'to' skipping weekends and add each date to array $current_date = $leave->from; $notes = "on leave"; if ($leave->notes) { $notes = $leave->notes; } while ($current_date->lte($leave->to)) { //echo $leave->id . " E:" . $leave->company_id . ' D:' . $current_date->format('Y-m-d') . '<br>'; if (array_key_exists($leave->company_id, $company_leave)) { // if not in array then add otherwise increment number of occurances if (!array_key_exists($current_date->format('Y-m-d'), $company_leave[$leave->company_id])) { $company_leave[$leave->company_id][$current_date->format('Y-m-d')] = $notes; } } else { $array[$current_date->format('Y-m-d')] = $notes; $company_leave[$leave->company_id] = $array; } $current_date->addDay(); } } return $company_leave; }