$this->executeSearch($this->query), ]); } /** * Execute the search against each of the searchable models. * * @return array>> */ protected function executeSearch(string $query): array { $query = Str::trim($query); $results = ['data' => [], 'total' => 0]; if (Str::length($query) > 0) { $results['data'] = [ 'user' => $this->fetchUserResults($query), 'mod' => $this->fetchModResults($query), ]; $results['total'] = $this->countTotalResults($results['data']); } $this->noResults = $results['total'] === 0; return $results; } /** * Fetch the user search results. * * @return Collection> */ protected function fetchUserResults(string $query): Collection { /** @var array> $userHits */ $userHits = User::search($query)->raw()['hits']; return collect($userHits); } /** * Fetch the mod search results. * * @return Collection> */ protected function fetchModResults(string $query): Collection { /** @var array> $modHits */ $modHits = Mod::search($query)->raw()['hits']; return collect($modHits); } /** * Count the total number of results across all models. * * @param array>> $results */ protected function countTotalResults(array $results): int { return collect($results)->reduce(function (int $carry, Collection $result) { return $carry + $result->count(); }, 0); } }