query('q', '')); // The search overlay's live results: the same endpoint, asked for by // its as the shopper types. if (request()->header('Turbo-Frame') === 'search-suggestions') { return $this->suggestions($query); } if ($query === '') { return redirect()->route('products'); } $listing = ProductListing::fromRequest(request()); $data = $this->listingPage->build( $listing, fn (array $overrides) => route('search', ['q' => $query] + $overrides), collectionId: null, query: $query, ); return view('search.index', [...$data, 'query' => $query]); } /** * The first few matches and how many there are in all — the dropdown * under the search overlay. Empty below LIVE_MIN_CHARS characters. */ private function suggestions(string $query) { $products = collect(); $total = 0; if (mb_strlen($query) >= self::LIVE_MIN_CHARS) { $result = $this->search->search($query, perPage: self::LIVE_LIMIT); $products = collect($result->products->items())->map(ProductCard::fromIndexed(...)); $total = $result->products->total(); } return view('search.suggestions', [ 'query' => $query, 'products' => $products, 'total' => $total, 'searched' => mb_strlen($query) >= self::LIVE_MIN_CHARS, ]); } }