PHP code example of defstudio / filament-searchable-input
1. Go to this page and download the library: Download defstudio/filament-searchable-input library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
defstudio / filament-searchable-input example snippets
use DefStudio\SearchableInput\Forms\Components\SearchableInput;
class ProductResource
{
public static function form(Form $form): Form
{
return $form->schema([
SearchableInput::make('description')
->options([
'Lorem ipsum dolor',
'Aspernatur labore qui fugiat',
'Dolores tempora libero assumenda',
'Qui rem voluptas officiis ut non',
//..
])
]);
}
}
use DefStudio\SearchableInput\Forms\Components\SearchableInput;
class ProductResource
{
public static function form(Form $form): Form
{
return $form->schema([
SearchableInput::make('description')
->options([
'Lorem ipsum dolor' => '[A001] Lorem ipsum dolor.',
'Aspernatur labore qui fugiat' => '[A001] Aspernatur labore qui fugiat.',
'Dolores tempora libero assumenda' => '[A002] Dolores tempora libero assumenda.',
'Qui rem voluptas officiis ut non' => '[A003] Qui rem voluptas officiis ut non.',
//..
])
]);
}
}
use DefStudio\SearchableInput\Forms\Components\SearchableInput;
class ProductResource
{
public static function form(Form $form): Form
{
return $form->schema([
SearchableInput::make('description')
->searchUsing(function(string $search){
return Product::query()
->where('description', 'like', "%$search%")
->orWhere('code', 'like', "%$search%")
->limit(15)
->pluck('description')
->values()
->toArray();
// Or, an associative array as well...
return Product::query()
->where('description', 'like', "%$search%")
->orWhere('code', 'like', "%$search%")
->limit(15)
->mapWithKeys(fn(Product $product) => [
$product->description => "[$product->code] $product->description"
])
->toArray();
// Or, also, an array of complex items (see below)
})
]);
}
}
use DefStudio\SearchableInput\Forms\Components\SearchableInput;
use DefStudio\SearchableInput\DTO\SearchResult;
class ProductResource
{
public static function form(Form $form): Form
{
return $form->schema([
SearchableInput::make('description')
->searchUsing(function(string $search){
return Product::query()
->where('description', 'like', "%$search%")
->limit(15)
->map(fn(Product $product) => SearchResult::make($product->description, "[$product->code] $product->description")
->withData('product_id', $product->id)
->withData('product_code', $product->code)
)
->toArray()
})
->onItemSelected(function(SearchResult $item){
$item->value();
$item->label();
$item->get('product_id');
$item->get('product_code');
}),
]);
}
}
SearchableInput::make('description')
->searchUsing(function(string $search, array $options){ //options defined in ->options([...])
//...
})
->searchUsing(function(string $search, Get $get, Set $set){ //$get and $set utilities
//...
})
->searchUsing(function(string $search, $state){ //current field state
//...
})
->searchUsing(function(string $search, Component $component){ //current component instance
//...
})
->searchUsing(function(string $search, ?Model $record){ //current form record
//...
})
->searchUsing(function(string $search, string $operation){ //current form operation (create/update)
//...
});