model->newQuery()->with(['employee']); // Apply filters if (!empty($filters['search'])) { $query->whereHas('employee', function ($q) use ($filters) { $q->search($filters['search']); }); } if (isset($filters['valid_authorization'])) { if ($filters['valid_authorization']) { $query->withValidAuthorization(); } else { $query->withExpiredAuthorization(); } } // Apply sorting $sortField = $filters['sort_by'] ?? 'created_at'; $sortDirection = $filters['sort_direction'] ?? 'desc'; $query->orderBy($sortField, $sortDirection); return $query->get(); } /** * Find a thanatopractitioner by ID. */ public function findById(int $id): ?Thanatopractitioner { return $this->model->newQuery() ->with(['employee', 'documents']) ->find($id); } /** * Find a thanatopractitioner by employee ID. */ public function findByEmployeeId(int $employeeId): ?Thanatopractitioner { return $this->model->newQuery() ->with(['employee', 'documents']) ->where('employee_id', $employeeId) ->first(); } /** * Get thanatopractitioners with valid authorization. */ public function getWithValidAuthorization(): Collection { return $this->model->newQuery() ->with(['employee']) ->withValidAuthorization() ->orderBy('authorization_expiry_date') ->get(); } /** * Get thanatopractitioners with expired authorization. */ public function getWithExpiredAuthorization(): Collection { return $this->model->newQuery() ->with(['employee']) ->withExpiredAuthorization() ->orderBy('authorization_expiry_date', 'desc') ->get(); } /** * Get thanatopractitioners with their complete data. */ public function getWithRelations(): Collection { return $this->model->newQuery() ->with(['employee', 'documents']) ->orderBy('created_at', 'desc') ->get(); } /** * Get thanatopractitioners with pagination. */ public function getPaginated(int $perPage = 10): array { $paginator = $this->model->newQuery() ->with(['employee']) ->paginate($perPage); return [ 'thanatopractitioners' => $paginator->getCollection(), 'pagination' => [ 'current_page' => $paginator->currentPage(), 'last_page' => $paginator->lastPage(), 'per_page' => $paginator->perPage(), 'total' => $paginator->total(), ], ]; } /** * Get thanatopractitioner statistics. */ public function getStatistics(): array { return [ 'total' => $this->model->newQuery()->count(), 'with_valid_authorization' => $this->model->newQuery()->withValidAuthorization()->count(), 'with_expired_authorization' => $this->model->newQuery()->withExpiredAuthorization()->count(), 'with_documents' => $this->model->newQuery()->has('documents')->count(), ]; } /** * Search thanatopractitioners by employee name. */ public function searchByEmployeeName(string $query): Collection { return $this->model->newQuery() ->with(['employee']) ->whereHas('employee', function ($q) use ($query) { $q->where('first_name', 'LIKE', "%{$query}%") ->orWhere('last_name', 'LIKE', "%{$query}%") ->orWhereRaw("CONCAT(first_name, ' ', last_name) LIKE ?", ["%{$query}%"]); }) ->limit(10) ->get(); } }