with(['collections', 'variants.prices']); } public function toSearchableArray(Model $model): array { /** @var Product $model */ $data = parent::toSearchableArray($model); $data['collections'] = $model->collections->pluck('id')->map(fn ($id) => (string) $id)->all(); $data['price'] = $this->cheapestPrice($model); return $data; } /** * The cheapest variant's base price (no customer group) in the default currency, * as a float in major units — e.g. 19.99, not 1999. Null if the product has no * variant with a price in that currency yet, so it's excluded from price filters * rather than sorting to the bottom as if it were free. */ private function cheapestPrice(Product $model): ?float { $currency = Currency::getDefault(); $price = $model->variants ->flatMap(fn ($variant) => $variant->prices) ->filter(fn ($price) => $price->currency_id === $currency->id && $price->customer_group_id === null) ->min(fn ($price) => $price->price->value); return $price !== null ? $price / (10 ** $currency->decimal_places) : null; } }