From 4398d5e27cd0d6201129cd24d3429dd5a1b0e76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Thu, 26 Feb 2026 21:55:27 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[attendance]=20User=20=EB=AA=A8=EB=8D=B8?= =?UTF-8?q?=EC=97=90=20tenantProfiles=20=EA=B4=80=EA=B3=84=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=A1=9C=20500=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - User 모델에 tenantProfiles() HasMany 관계 추가 (tenant_user_profiles 테이블) - eager loading에 department 관계도 포함하여 N+1 방지 --- app/Models/User.php | 8 ++++++++ app/Services/HR/AttendanceRequestService.php | 2 +- app/Services/HR/AttendanceService.php | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index f8786e18..9240da23 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -195,6 +195,14 @@ public function currentTenant() return $this->tenants()->find($tenantId); } + /** + * 관계: 테넌트별 프로필 (tenant_user_profiles 테이블) + */ + public function tenantProfiles(): HasMany + { + return $this->hasMany(\App\Models\HR\Employee::class, 'user_id'); + } + /** * 관계: 사용자-역할 (user_roles 테이블, 테넌트별) */ diff --git a/app/Services/HR/AttendanceRequestService.php b/app/Services/HR/AttendanceRequestService.php index 0ec06757..8bfc534a 100644 --- a/app/Services/HR/AttendanceRequestService.php +++ b/app/Services/HR/AttendanceRequestService.php @@ -18,7 +18,7 @@ public function getRequests(array $filters = [], int $perPage = 20): LengthAware $tenantId = session('selected_tenant_id'); $query = AttendanceRequest::query() - ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId), 'approver']) + ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId), 'user.tenantProfiles.department', 'approver']) ->forTenant($tenantId) ->orderByRaw("FIELD(status, 'pending', 'approved', 'rejected')") ->orderBy('created_at', 'desc'); diff --git a/app/Services/HR/AttendanceService.php b/app/Services/HR/AttendanceService.php index ceff5bd9..d5f66d22 100644 --- a/app/Services/HR/AttendanceService.php +++ b/app/Services/HR/AttendanceService.php @@ -21,7 +21,7 @@ private function buildFilteredQuery(array $filters = []) $tenantId = session('selected_tenant_id'); $query = Attendance::query() - ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId)]) + ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId), 'user.tenantProfiles.department']) ->forTenant($tenantId); if (! empty($filters['q'])) { @@ -327,7 +327,7 @@ public function getMonthlyCalendarData(int $year, int $month, ?int $userId = nul $endDate = Carbon::create($year, $month, 1)->endOfMonth()->toDateString(); $query = Attendance::query() - ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId)]) + ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId), 'user.tenantProfiles.department']) ->forTenant($tenantId) ->betweenDates($startDate, $endDate) ->orderBy('base_date')