F
Filament15mo ago
roni

how to mutate data in record in relation in form

I have a userResource for User table which has a metadata table with its foreign-key. I have added form fields of metadata with Users fields in one form like this:
return $form
->schema([
//user table field...
...
Fieldset::make('Metadata')
->relationship('metadata')
->schema([
Forms\Components\TextInput::make('experience')
->required()
->maxLength(255),
]),
return $form
->schema([
//user table field...
...
Fieldset::make('Metadata')
->relationship('metadata')
->schema([
Forms\Components\TextInput::make('experience')
->required()
->maxLength(255),
]),
I want to mutate "experience" field of metadata before filling this form. I tried mutateRecordDataUsing, but it is not working, even dd($data) is not triggering.
->actions([
Tables\Actions\EditAction::make()->label('Edit')
->mutateRecordDataUsing(function (array $data): array {
dd($data);
}
->actions([
Tables\Actions\EditAction::make()->label('Edit')
->mutateRecordDataUsing(function (array $data): array {
dd($data);
}
How do I do this?
8 Replies
LeandroFerreira
LeandroFerreira15mo ago
Can you try mutateFormDataUsing ?
roni
roni15mo ago
I guess mutateFormDataUsing is to mutate data before storing it. I also tried that but it is not working
LeandroFerreira
LeandroFerreira15mo ago
ahh sorry, before filling the form...
josef
josef15mo ago
try using ->saveRelationShipDataUsing() oh, before, then load instead of save
LeandroFerreira
LeandroFerreira15mo ago
mutateRecordDataUsing should work I think What about an accessor? https://laravel.com/docs/10.x/eloquent-mutators#defining-an-accessor
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
roni
roni15mo ago
mutateRecordDataUsing works fine on RelationManager for metadata table but it is not working on UserResource.
LeandroFerreira
LeandroFerreira15mo ago
You can try an accessor I think...
roni
roni15mo ago
Yes Thanks, That works, but that applies mutation globally. If any other part of application tries to access that field than it will get modified data. In that case I have to remove my RelationManagers mutators. I was looking to mutate locally.