Correct way to transform field data for CRUD create/update? #536
-
Let's say I have a model If I want to use the month field, what is the correct way to transform this data so that it properly splits this and sets the I can get this to work for creation by defining a
but of course this doesn't work when
I'm wondering what is the correct way to transform data in the CrudController? Or would the better way be to adjust the database to hold the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi mate @alancwoo , try this one so we have public function getCleanYearAttribute()
{
return explode('-',$this->year)[0];
}
protected function year(): Attribute
{
return Attribute::make(
get: fn (string $value) => $value.'-'.$this->month,
);
} and your //this is for setupListOperation
CRUD::column('clean_year'); //change year to clean_year
//this is for setupCreateOperation
CRUD::field('year')->type('month')->on('saving', function ($entry) {
$explodeData = explode('-', $entry->year);
$entry->year = $explodeData[0];
$entry->month = $explodeData[1];
}); Reference for now, that's from me, let me know if you find any issues, ill try to find another way and update it here, so we can improve it. Cheer |
Beta Was this translation helpful? Give feedback.
Hi mate @alancwoo , try this one
so we have
Issue
model, create a laravel accessor inside yourIssue
model, you can add this functionand your
IssueCrudController
should be like this