user_name; } public function getUserKtpNumberAttribute() { return $this->user_ktp_number; } private function assignUserInfo($users) { foreach ($users as $user) { if ($user->id == $this->user_id) { //Log::debug("User ID matched {$user->id}"); $this->setUserInfo($user); break; } } } private function setUserInfo($user) { $this->user_name = $user->name; $this->user_ktp_number = $user->ktp_number; } public static function deleteYYYYMM($yyyymm) { $rawWhere = DB::raw("to_char(start_at + (end_at - start_at) / 2, 'YYYYMM')"); $res = UserMonthlyShift::where($rawWhere, $yyyymm)->delete(); return $res; } // get monthly schedules public static function monthlyWithUserInfo($yyyymm) { // fetch schedule with user name and KTP no // get user_id that match the condition $rawWhere = DB::raw("to_char(start_at+INTERVAL '1 day', 'YYYYMM')"); $userIds = UserMonthlyShift::where($rawWhere, $yyyymm)->select('user_id'); // Fetch users from database $users = User::whereIn('id', $userIds) ->orderBy('id') ->select('id', 'name', 'ktp_number') ->get(); //var_dump($users); //Log::info($users); // Get all schedules $schedules = UserMonthlyShift::where($rawWhere, $yyyymm)->get(); if ($schedules) { foreach($schedules as $schedule) { $schedule->assignUserInfo($users); //Log::debug($schedule); } } //Log::debug($schedules); return $schedules; } }