F
Filament15mo ago
Wim

Radio button options stores first digit only

I have a radio button as follows

Radio::make('age')
->options([
'0-1 years',
'1-2 years',
....
'9-10 years',
'older than 10 years',
'older than 15 years',
])

Radio::make('age')
->options([
'0-1 years',
'1-2 years',
....
'9-10 years',
'older than 10 years',
'older than 15 years',
])
The corresponding fields in the database is a varchar. Filemant saves it as 0 (for 0-1 years), 1 for (1-2 years), .... 11 (for older than 10 years). What can I do so it is saved literally as '0-1 years', '1-2 years',...?
3 Replies
Lara Zeus
Lara Zeus15mo ago
what is the type of column in the DB and do you have cast attribute in the model
DrByte
DrByte15mo ago
Is it because your 'age' field in the db is 1-char ? You said it's varchar, but didn't say its length. And like LaraZeus asked, is there a cast that's converting it to something else? Does it make any difference if you pass the array a key=>value instead of just value? eg:
->options([
'0-1 years' => '0-1 years',
'1-2 years' => '1-2 years',
...
->options([
'0-1 years' => '0-1 years',
'1-2 years' => '1-2 years',
...
(ya, the duplication isn't ideal, but does it make any difference to make the change? That may give clues to the solution) And you could explore using an Enum for it so you don't have to maintain the options in more than one place in your app. But that can be considered later...
Wim
WimOP15mo ago
The corresponding fields in the database is a varchar(100) I use in the model a cast to 'string' for that column. When I do this I get an error "Array to string conversion" This is with
->options([
'0-1 years' => '0-1 years',
'1-2 years' => '1-2 years',
->options([
'0-1 years' => '0-1 years',
'1-2 years' => '1-2 years',
as well as:
->options([
'0-1 years',
'1-2 years',
->options([
'0-1 years',
'1-2 years',
and with a cast as follows
'age' => 'string',
'age' => 'string',
in the model It was solved by using
->options([
'0-1 years' => '0-1 years',
'1-2 years' => '1-2 years',
->options([
'0-1 years' => '0-1 years',
'1-2 years' => '1-2 years',
but doing no cast at all in the model
Want results from more Discord servers?
Add your server