Mutate relation data before save

I'm trying to mutate my data of a relationmanager before I save the records (attach). The attach should get an extra parameter of the 'parent' in the relation that has some data, so I can save/mutate the data. It should also hook into another function so that I can update other data. For example; In the relation manager I have this;
Tables\Actions\AttachAction::make()->preloadRecordSelect()->beforeFormFilled(function (array $data) {
dd($data);
}),
Tables\Actions\AttachAction::make()->preloadRecordSelect()->beforeFormFilled(function (array $data) {
dd($data);
}),
I don't see any DD being triggered here. I want, when the user presses attach, that there is data being added to the attaching object. Plus that there is another function that is being called to "sync" other data. Any ideas?
4 Replies
Dan Harrin
Dan Harrin2y ago
i think it should be before() instead there is no data to fill the form with on the AttachAction
Robin
RobinOP2y ago
I've modified the code slightly, it now looks like this;
->headerActions([
Tables\Actions\AttachAction::make()
->mutateFormDataUsing(function (array $data): array {
// add the zone ID of the bar here
$data['zone_id'] = $this->ownerRecord->zone_id;
return $data;
})
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('start_time')
->numeric()
->required(),
Forms\Components\TextInput::make('preparation_time')
->numeric()
->required()
])
->preloadRecordSelect(),
])
->headerActions([
Tables\Actions\AttachAction::make()
->mutateFormDataUsing(function (array $data): array {
// add the zone ID of the bar here
$data['zone_id'] = $this->ownerRecord->zone_id;
return $data;
})
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('start_time')
->numeric()
->required(),
Forms\Components\TextInput::make('preparation_time')
->numeric()
->required()
])
->preloadRecordSelect(),
])
However, the $this->ownerRecord->zone_id doesn't work, since it's using $this when not in object context it works when I fill in a static number, so I just need some way to get the "base" model data in there
Dan Harrin
Dan Harrin2y ago
->mutateFormDataUsing(function (array $data, RelationManager $livewire): array {
// add the zone ID of the bar here
$data['zone_id'] = $livewire->ownerRecord->zone_id;
return $data;
})
->mutateFormDataUsing(function (array $data, RelationManager $livewire): array {
// add the zone ID of the bar here
$data['zone_id'] = $livewire->ownerRecord->zone_id;
return $data;
})
Robin
RobinOP2y ago
Awesome! That works! Thanks ❤️
Want results from more Discord servers?
Add your server