attendanceRepository = $attendanceRepository; $this->employeeRepository = $employeeRepository; } public function monthly() { $data['title'] = 'Employee Attendance Lists'; $firstDayOfMonth = Carbon::createFromDate(date('Y'), date('m'), 1)->startOfMonth(); $lastDayOfMonth = Carbon::createFromDate(date('Y'), date('m'), 1)->endOfMonth(); for ($day = $firstDayOfMonth; $day <= $lastDayOfMonth; $day->addDay()) { $data['dayLists'][] = $day->format('Y-m-d'); } $data['employees'] = $this->employeeRepository->findAll() ->transform(function ($employee) use ($data) { $attendances = Attendance:: where('employee_id', $employee->id) ->whereIn('date', $data['dayLists']) ->select('clock_in_time', 'clock_out_time', 'date') ->get() // ->groupBy('date') ->mapWithKeys(function ($atd) { return [$atd['date'] => 'P']; }); $leave = Leave:: where('employee_id', $employee->id) ->whereIn('start_date', $data['dayLists']) ->whereStatus(2) ->get() ->mapWithKeys(function ($atd) { return [$atd['start_date'] => 'L']; }); $final = $attendances->push($leave); $attendanceArray = []; foreach ($data['dayLists'] as $date) { $attendanceArray[$date] = array_key_exists($date, $final->toArray()) ? $final[$date] : ''; } $employee->dates = $attendanceArray; return $employee; }); return view('attendance::report.monthly', $data); } }