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
Can you try mutateFormDataUsing ?
rajul
rajulOP2y ago
I guess mutateFormDataUsing is to mutate data before storing it. I also tried that but it is not working
LeandroFerreira
ahh sorry, before filling the form...
josef
josef2y ago
try using ->saveRelationShipDataUsing() oh, before, then load instead of save
LeandroFerreira
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.
rajul
rajulOP2y ago
mutateRecordDataUsing works fine on RelationManager for metadata table but it is not working on UserResource.
LeandroFerreira
You can try an accessor I think...
rajul
rajulOP2y 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.
Want results from more Discord servers?
Add your server