Filament PHP : handle SelectMultiple text field in table column and filters

a very nice prototyping feature given by FilamentPHP is the possibility to create select multiple values in the form with options from a class scoped array , without creating BelongToMany relationships that involve an extra table. The kind of issue for which sushi package was created, but we don't need that here, or I might say, not yet (check out this discussion).

Of course, using relation tables is the core of application development, but in the course of application prototyping it can happen that you want to create a field with multiple possible values without creating the extra tables, which in the case of multiple select involve the reference table and the pivot table. SO to avoid this extra work if you're not sure of what you want, use the select with multiple option and a class constant for the options, you'll have your multiple field select options demonstration ready :

 const   arrOptions=[ ... ]

...
use Filament\Forms\Components\Select;
...
Select::make('technologies')
->multiple()
->options( self::arrOptions)

the thing is that Filament has not native equivalent of this field in the table list. You need to use functions to display list of options for a particular entry , especially the formatStateUsing method, html formatted to allow the use of breaking lines BR tag :

       Tables\Columns\TextColumn::make('options') ->label('Options')
->formatStateUsing(function ($state, $record) {
return join("<br>",$record->options); //
}) ->html(),

now you might also want to filter entries that have one of the values, remember you have cast the field as array in your table Eloquent model, so it is stored as a string in PHP Array style. So a like clause will do in the context of custom filter, great feature of FILAMENT !!

     Filter::make('options')->label('options')
->form([
select::make('optionSelection') ->options( self::arrOptions ),

])

->query(function (Builder $query, array $data): Builder {
$value = $data['optionSelection'];
if ($value)

$query = $query->orWhere('options', 'like','%' . $value . '%');

return $query;
})
To top