FilamentF
Filament3y ago
6 replies
urbycoz

Is there a way to use Toggle component for yes/no strings instead of booleans?

For some reason the table I am using has an is_admin field where the values are not booleans but instead text saying "yes" or "no".

When I try to save when I use....
Toggle::make('is_admin')
....I (understandably) get this error:
Invalid text representation: 7 ERROR: invalid input value: "0" CONTEXT: unnamed portal parameter $1 = '...'
update "user" SET "is_admin" = 0 WHERE "id" = 31


(Note: I realise the ideal solution would be to redesign the table but there would be a lot of refactoring involved which I don't have time for right now.)
Solution
Just use a Laravel custom attribute in your model?

something like:
    public function getis_adminAttribute($value)
    {
        return $value === 'yes' ? true : false;
    }
Was this page helpful?