Creating multiple select field, reading from and saving to string field in db, using enums?
I have created my select field:
Select::make('velicinaStandard')
->multiple()
->options(StandardneVelicine::cases())
->label('Veličina standard'),
Options is using my enum:
<?php
namespace App\Enums;
enum StandardneVelicine : string
{
case M = 'M';
case L = 'L';
case XL = 'XL';
case XXL = 'XXL';
}
and regarding my velicinaStandard which is String with value like: M,L,XL I added get set to my MODEL (maybe this is the wrong place?):
protected $casts = [
'velicinaStandard' => 'array',
];
protected function velicinaStandard(): Attribute
{
return Attribute::make(
get: fn ($value) => explode(',', $value),
set: fn ($value) => implode(',', $value),
);
}
The options are there but my value from velicinaStandard is not showing up in selected options.
What am I doing wrong?2 Replies
the select expects an array, not comma separated stirng
so you dont need the attribute
I solved it by using mutateFormDataBeforeFill, mutateFormDataBeforeSave and mutateFormDataBeforeCreate. Thanks anyway.