chosenId(); $property = $chosen !== null ? Property::query()->find($chosen) : null; return $property ?? Property::query()->where('is_active', true)->orderBy('id')->first() ?? Property::query()->orderBy('id')->first(); } public function id(): ?int { return $this->get()?->id; } public function select(Property $property): void { if (app()->bound('session')) { session()->put(self::SESSION_KEY.'.'.$this->tenantKey(), $property->id); } } /** * An apartment named in a page URL (old links, e-mails) becomes the choice; another landlord's apartment is not found. */ public function followUrl(?int $propertyId): ?int { if ($propertyId === null) { return $this->id(); } $this->select(Property::query()->findOrFail($propertyId)); return $propertyId; } /** * Whether the landlord has more than one apartment to choose from. */ public function hasChoice(): bool { return Property::query()->count() > 1; } /** * @return array */ public function options(): array { return Property::query()->orderBy('name')->get() ->mapWithKeys(fn (Property $property): array => [$property->id => $property->is_active ? $property->name : $property->name.' (inaktiv)']) ->all(); } private function chosenId(): ?int { if (! app()->bound('session')) { return null; } $id = session()->get(self::SESSION_KEY.'.'.$this->tenantKey()); return is_numeric($id) ? (int) $id : null; } private function tenantKey(): string { return (string) (app(TenantContext::class)->id() ?? 'none'); } }