fix: [attendance] User 모델에 tenantProfiles 관계 추가로 500 에러 수정
- User 모델에 tenantProfiles() HasMany 관계 추가 (tenant_user_profiles 테이블) - eager loading에 department 관계도 포함하여 N+1 방지
This commit is contained in:
@@ -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 테이블, 테넌트별)
|
||||
*/
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user