Simple (modal) resource and mutateFormDataBeforeSave()

I have a problem with my simple resource. I need to mutate the data before saving, but it's not possible to use mutateFormDataBeforeSave() as I would do with normal resource. Any ideas? I would like to add the following code:
$data['user_id'] = Auth::user()->id;
$data['user_id'] = Auth::user()->id;
I could use the boot() method on the model, but I would prefer not to do that. Thank you ZioLupo
Solution:
Try adding ->using() to the Filament\Tables\Actions\EditAction https://filamentphp.com/docs/3.x/actions/prebuilt-actions/edit#customizing-the-saving-process ```php use Illuminate\Database\Eloquent\Model; ...
Jump to solution
2 Replies
Solution
tuto1902
tuto190211mo ago
Try adding ->using() to the Filament\Tables\Actions\EditAction https://filamentphp.com/docs/3.x/actions/prebuilt-actions/edit#customizing-the-saving-process
use Illuminate\Database\Eloquent\Model;

EditAction::make()
->using(function (Model $record, array $data): Model {
$data['user_id'] = Auth::user()->id

$record->update($data);

return $record;
})
use Illuminate\Database\Eloquent\Model;

EditAction::make()
->using(function (Model $record, array $data): Model {
$data['user_id'] = Auth::user()->id

$record->update($data);

return $record;
})
ziolupo
ziolupoOP11mo ago
Thanks to your suggestion I was able to fine the right way. My SimpleResource is called Task and in the 'ManageTasks.php' file I added:
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();
return $data;
}),
];
}
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();
return $data;
}),
];
}
The above function take care of inserting the user_id during the creation process. Then I did the same in TaskResource taking care of the saving process:
->actions([
Tables\Actions\EditAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();
return $data;
}),
->actions([
Tables\Actions\EditAction::make()
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = auth()->id();
return $data;
}),
Thank you so much!
Want results from more Discord servers?
Add your server