F
Filamentβ€’4w ago
foggy

TextColumn depending on database value....

Should be a simple one. I have a database table column "status".. that statuses are encoded as 1,2,3,4,5 and stored as an int. On the Form used to create the table row, I have a Select:: thus:
Select::make('status')
->label('Status')
->required()
->options([
'1' => 'Open',
'2' => 'Closed',
'3' => 'Waiting',
'4' => 'This',
'5' => 'That
]),
Select::make('status')
->label('Status')
->required()
->options([
'1' => 'Open',
'2' => 'Closed',
'3' => 'Waiting',
'4' => 'This',
'5' => 'That
]),
...and this works. I render the page, hit the drop down, I see the options "Open", "Closed" etc but it's the number 1, 2, 3 etc goes in the database. Great! My issue is then how do I display that on the table? I can't find a code snippet to plagiarise to do this! How do I display "Open", "Closed" etc depending on 1, 2, 3 etc in the database? TIA!
Solution:
I'd use mutators/accessors to centralise that transformation
Jump to solution
5 Replies
Solution
James
Jamesβ€’4w ago
I'd use mutators/accessors to centralise that transformation
akahalls
akahallsβ€’3w ago
Use Enums
foggy
foggyOPβ€’3w ago
looking into both these (thanks). If I'm right, using enums will hardcode the text into the db. I want to be able to change the text down the road. Looks like mutators/accessors is the 'proper' way to do it, looking for a relevant example now πŸ™‚
awcodes
awcodesβ€’3w ago
you can still use non string backed enums and just cast it on the model.
foggy
foggyOPβ€’3w ago
Thanks for the pointers, got it working through accessors πŸ™‚

Did you find this page helpful?