with('supervisor:id,name')->find($id); return $model === null ? null : $this->toSummary($model); } public function paginate(?string $search, int $perPage = 25): LengthAwarePaginator { $search = trim((string) $search); return User::query() ->with('supervisor:id,name') ->when($search !== '', function (Builder $query) use ($search): void { $pattern = '%'.addcslashes($search, '%_\\').'%'; $query->where(fn (Builder $query) => $query ->where('name', 'like', $pattern) ->orWhere('email', 'like', $pattern)); }) ->orderBy('name') ->paginate($perPage) ->through(fn (User $model): UserSummary => $this->toSummary($model)); } public function supervisorOptions(?int $exceptUserId = null): array { return User::query() ->whereIn('role', array_filter(Role::cases(), fn (Role $role): bool => $role->canSupervise())) ->where('is_active', true) ->when($exceptUserId !== null, fn (Builder $query) => $query->whereKeyNot($exceptUserId)) ->orderBy('name') ->get(['id', 'name']) ->map(fn (User $model): UserOption => new UserOption($model->id, $model->name)) ->values() ->all(); } private function toSummary(User $model): UserSummary { return new UserSummary( id: $model->id, name: $model->name, email: $model->email, role: $model->role, active: $model->is_active, employmentEndsOn: $model->employment_ends_on?->toDateString(), supervisorId: $model->supervisor_id, supervisorName: $model->supervisor?->name, ); } }