> */ #[Locked] public array $result = []; /** * The total number of search results. */ #[Locked] public int $count = 0; /** * Render the component. */ public function render(): View { $this->result = $this->executeSearch($this->query); $this->count = $this->countTotalResults($this->result); return view('livewire.global-search'); } /** * Execute the search against each of the searchable models. * * @return array> */ protected function executeSearch(string $query): array { $query = Str::trim($query); if (Str::length($query) > 0) { return [ 'user' => $this->fetchUserResults($query), 'mod' => $this->fetchModResults($query), ]; } return []; } /** * Fetch the user search results. * * @return Collection */ protected function fetchUserResults(string $query): Collection { /** @var Collection $searchHits */ $searchHits = User::search($query)->raw()['hits']; return collect($searchHits); } /** * Fetch the mod search results. * * @return Collection */ protected function fetchModResults(string $query): Collection { /** @var Collection $searchHits */ $searchHits = Mod::search($query)->raw()['hits']; return collect($searchHits); } /** * Count the total number of results across all models. * * @param array> $results */ protected function countTotalResults(array $results): int { return (int) collect($results)->reduce(fn (int $carry, Collection $result): int => $carry + $result->count(), 0); } }