FilamentF
Filament16mo ago
Venky

How to enable search in custom options

Select::make('products')
    ->reactive()
    ->searchable()
    ->multiple()
    ->options(function (Get $get, Set $set, ?string $search = null) {
        $results = null;
        if (!is_null($get('category_id'))) {
            $query = DB::table('product_flat')
            if ($search) {
                $query->where('product_flat.name', 'like', '%' . $search . '%');
            }
            $products = $query->get();
            $productHtmlArray = [];
            foreach ($products as $product) {
                $priceHtml = $product->special_price && now()->between($product->special_price_from, $product->special_price_to)
                    ? '<span class="text-red-500">$' . htmlspecialchars($product->special_price) . '</span> <span class="line-through text-gray-500">$' . htmlspecialchars($product->price) . '</span>'
                    : '<span>$' . htmlspecialchars($product->price) . '</span>';
                $html = '
                    <div class="p-4" style="width: 350px;"></div>';
                $productHtmlArray[$product->product_id] = $html;
            }
            return $productHtmlArray;
        }
        return [];
    })
    ->searchDebounce(500)
    ->searchResultsLimit(10)
    ->placeholder('Select Products');
Was this page helpful?