Duplicating record before using inside of editOptionForm.

Hi all, I would like to duplicate a record before opening it in the modal and editing it using the editOptionForm. I have been playing around with all the various lifecyclehooks of the editOptionAction -> $action, but without any success at the moment. I can't seem to find how to pass the newly created record to the Component of the Edit action / form? Current code (not working):
->editOptionForm(FinancialCalculation::form())
->editOptionAction(function(Forms\Components\Actions\Action $action, Forms\Components\Select $component, $livewire) {
$form_data = $component->getSelectedRecord()?->attributesToArray();

if (is_array($form_data)) {
$form_data['is_hidden'] = 1;
$form_data['is_disabled'] = 1;

$action->fillForm(function(Forms\Components\Select $component) use ($form_data) {
$record = $component->getSelectedRecord()?->replicate();
return $record->attributesToArray();
});
}

return $action->slideOver();
})
->editOptionForm(FinancialCalculation::form())
->editOptionAction(function(Forms\Components\Actions\Action $action, Forms\Components\Select $component, $livewire) {
$form_data = $component->getSelectedRecord()?->attributesToArray();

if (is_array($form_data)) {
$form_data['is_hidden'] = 1;
$form_data['is_disabled'] = 1;

$action->fillForm(function(Forms\Components\Select $component) use ($form_data) {
$record = $component->getSelectedRecord()?->replicate();
return $record->attributesToArray();
});
}

return $action->slideOver();
})
Anyone have any ideas? Thanks!
3 Replies
Auth1Specialist
Auth1SpecialistOP6d ago
Some more tries I did whitout success:
->editOptionAction(function(Forms\Components\Actions\Action $action, Forms\Components\Select $component, $livewire) {
$selectedRecord = $component->getSelectedRecord();

if ($selectedRecord) {
$duplicatedRecord = $selectedRecord->replicate();
$duplicatedRecord->project_id = $livewire->project_id;
$duplicatedRecord->is_hidden = 1;
$duplicatedRecord->is_disabled = 1;

$action->fillForm($duplicatedRecord->attributesToArray());
}

return $action->slideOver();
})
->editOptionAction(function(Forms\Components\Actions\Action $action, Forms\Components\Select $component, $livewire) {
$selectedRecord = $component->getSelectedRecord();

if ($selectedRecord) {
$duplicatedRecord = $selectedRecord->replicate();
$duplicatedRecord->project_id = $livewire->project_id;
$duplicatedRecord->is_hidden = 1;
$duplicatedRecord->is_disabled = 1;

$action->fillForm($duplicatedRecord->attributesToArray());
}

return $action->slideOver();
})
LeandroFerreira
->editOptionAction(fn (Forms\Components\Actions\Action $action) =>
$action->mountUsing(function (YourModel $record) {
$record
->replicate()
->save();
})
)
->editOptionAction(fn (Forms\Components\Actions\Action $action) =>
$action->mountUsing(function (YourModel $record) {
$record
->replicate()
->save();
})
)
Auth1Specialist
Auth1SpecialistOP5d ago
Thanks for the respons! Will try it out later!

Did you find this page helpful?