DEV Community

Horacio Degiorgi
Horacio Degiorgi

Posted on

Dynamic datalist in filamentphp 3

Revised version , thanks Filament Team !!

The new version of filamentphp allow the updating of datalist tag in TexInput with new methods. Some tricks are needed but is possible to load datalist options from the database based on the characters inserted in input field.
The tricks:
->autocomplete('off') avoid browser tips
->live(debounce:400) activate sending info to the server
->datalist(function (?string $state, TextInput $component,?Model $record , $modelsearch='\App\Models\Metadata' , $fieldsearch='content') {

$options= $modelsearch::whereRaw($fieldsearch.' ilike \'%'.$state.'%\'')->limit(20)->pluck('content')->toarray();


All the code (simplified)

                ->datalist(function (?string $state , Forms\Components\TextInput $component,?Model $record , $modelsearch='\App\Models\Metadata' , $fieldsearch='content') {
//get options from database using where and (ilike I'm a postgresql fan)
$options =[];
   if($state!=null  and Str::length($state)>=3){
                    $options= $modelsearch::whereRaw($fieldsearch.
' ilike \'%'.$state.'%\'')
               //send options to datalist

return $options; 

Enter fullscreen mode Exit fullscreen mode

Btw : PHP 8.0: Named Parameters ROCKS

Top comments (0)