F
Filament4mo 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');
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');
4 Replies
Dennis Koch
Dennis Koch4mo ago
Please read our #✅┊rules for how to use code formatting
Venky
VenkyOP4mo ago
Yes, I modified it.
Dennis Koch
Dennis Koch4mo ago
Instead of ->options() you can use ->getSearchResultsUsing() to search those.
Venky
VenkyOP4mo ago
@Dennis Koch Thanks a lot. Let me try It works perfectly. I hope the ->default() will render the same html. Let me try. Please mention here if we have any other approach. Thanks again for your support
Want results from more Discord servers?
Add your server