F
Filament4mo ago
CK

Select and TextColumn with Associative Array

use Filament\Forms\Components\Select;

Select::make('status')
->options([
-1 => 'Terminated',
0 => 'Pending',
1 => 'Approved',
]),
use Filament\Forms\Components\Select;

Select::make('status')
->options([
-1 => 'Terminated',
0 => 'Pending',
1 => 'Approved',
]),

May I make and external array like
$status = [
-1 => 'Terminated',
0 => 'Pending',
1 => 'Approved',
]
$status = [
-1 => 'Terminated',
0 => 'Pending',
1 => 'Approved',
]
Select::make('status')
->options($status),
Select::make('status')
->options($status),
or
Select::make('status')
->options($this->status),
Select::make('status')
->options($this->status),

and in
TextColumn::make('status'),
TextColumn::make('status'),
how to show the value in state of the key? eg. when selected -1, show the word 'Terminated' instate of -1. Thank you
5 Replies
Lara Zeus
Lara Zeus4mo ago
for better dx, I recommend to use an enum class https://filamentphp.com/docs/3.x/support/enums but in you case you can do like this
textcolumn::make('status')
->state(fn(string $state)=>$this-status[$state])
textcolumn::make('status')
->state(fn(string $state)=>$this-status[$state])
CK
CKOP3mo ago
Thanks for your information. Seems the enums is only available in Laravel 11 with php 8.2.x and above. But currently I'm using php 8.1.x and only allow to install Laravel 10.
toeknee
toeknee3mo ago
Enums are available with Laravel 10 and filamentphp 3 as I use them.
CK
CKOP3mo ago
Thanks for your information. I try running php artisan from Laravel 10 and it shown Command "make:enum" is not defined. So I created another VPS with php 8.2 & L11. The Command "make:enum" is ready there. Will copy the output from L11 to L10 and try it out.
toeknee
toeknee3mo ago
Make enum may not exist but that doesn’t mean it isn’t supported…. Just create your own enum class without the make function.
Want results from more Discord servers?
Add your server