Enum select key is not the constant but the value
I tried to make a
Select
field in my form with an Enum
. I used this documentation for it: https://filamentphp.com/docs/3.x/support/enums#enum-labels
But when it is saved the values writing into the database, not the constant, what is the expected behavior (I think).
Here is the Enum: https://github.com/sajt/iingatlanegyszer-filament/blob/main/app/Enums/County.php
Here is the Model (where is the form and the cast): https://github.com/sajt/iingatlanegyszer-filament/blob/main/app/Models/City.php
And of course the resource what is using getForm from the model: https://github.com/sajt/iingatlanegyszer-filament/blob/main/app/Filament/Resources/CityResource.php
So, when I save the form the county value is 'Budapest' not BUDAPESTGitHub
iingatlanegyszer-filament/app/Enums/County.php at main · sajt/iing...
Contribute to sajt/iingatlanegyszer-filament development by creating an account on GitHub.
GitHub
iingatlanegyszer-filament/app/Models/City.php at main · sajt/iinga...
Contribute to sajt/iingatlanegyszer-filament development by creating an account on GitHub.
GitHub
iingatlanegyszer-filament/app/Filament/Resources/CityResource.php a...
Contribute to sajt/iingatlanegyszer-filament development by creating an account on GitHub.
9 Replies
is not the constant but the valueThat's how Backed Enums work. It's always the value. Don't use a backed enum if you don't want a backed value. Why do you use Labels for your values?
It is mean the option value is the same the label?
<option value="value">value</option>
In the tinker:
In this i thinked the optiopn be like this:
<option value="PEST">Pest</option>
Is this not correct?
Oh I see this is a php feature. I have to read about it.No. Value is the value 😅
Yeah, Enums are PHP. Filament provides Interfaces so we can automatically use Labels, Colors, etc.
And what do you think, what is the best solution to store select datas like these in the database?
Just name the value like you want it to be stored.
Bat where stote the key=>value parts? Database or contig file or what?
I don't understand what you mean.
If you want Filament to generate Select options from your Enum it will use
value
as the option value (what you call key?) and getLabel()
as the option labelOk i try it. Thanks.
I think i understand now
Solution
I needed somyhing like this:
case BUDAPEST = 'BUDAPEST';