CreateAnother Retain Some Data

With the panel builder, CreateAction, how can i retain data when a user clicks the create another button? Ive seen quite a few different ways to go about it, but none of them seemed that clean or native. Right now I am not even doing a custom action() and hoping i dont have to redo that whole process.
2 Replies
Mark Chaney
Mark Chaney3mo ago
public static function createComparable($action, $form, $data, $livewire, $arguments): void
{
$model = $action->getModel();
$record = $action->process(function (array $data, Table $table) use ($model): Model {
$relationship = $table->getRelationship();
$pivotData = [];
if ($relationship instanceof BelongsToMany) {
$pivotColumns = $relationship->getPivotColumns();
$pivotData = Arr::only($data, $pivotColumns);
$data = Arr::except($data, $pivotColumns);
}
if ($translatableContentDriver = $table->makeTranslatableContentDriver()) {
$record = $translatableContentDriver->makeRecord($model, $data);
} else {
$record = new $model;
$record->fill($data);
}
if (
(! $relationship) ||
$relationship instanceof HasManyThrough
) {
$record->save();
return $record;
}
if ($relationship instanceof BelongsToMany) {
$relationship->save($record, $pivotData);
return $record;
}
/** @phpstan-ignore-next-line */
$relationship->save($record);
return $record;
});
$action->record($record);
$form->model($record)->saveRelationships();
$livewire->mountedTableActionRecord($record->getKey());

if ($arguments['another'] ?? false) {
$action->record(null);
// Ensure that the form record is anonymized so that relationships aren't loaded.
$form->model(self::class);
$livewire->mountedTableActionRecord(null);
$preservedData = Arr::only($data, ['property_id']);
$form->fill();
$form->fill($preservedData);
$action->successNotificationTitle('Comparable Unit added successfully');
$action->success();
$action->halt();
return;
}
$action->successNotificationTitle('Comparable Unit added successfully');
$action->success();
}
public static function createComparable($action, $form, $data, $livewire, $arguments): void
{
$model = $action->getModel();
$record = $action->process(function (array $data, Table $table) use ($model): Model {
$relationship = $table->getRelationship();
$pivotData = [];
if ($relationship instanceof BelongsToMany) {
$pivotColumns = $relationship->getPivotColumns();
$pivotData = Arr::only($data, $pivotColumns);
$data = Arr::except($data, $pivotColumns);
}
if ($translatableContentDriver = $table->makeTranslatableContentDriver()) {
$record = $translatableContentDriver->makeRecord($model, $data);
} else {
$record = new $model;
$record->fill($data);
}
if (
(! $relationship) ||
$relationship instanceof HasManyThrough
) {
$record->save();
return $record;
}
if ($relationship instanceof BelongsToMany) {
$relationship->save($record, $pivotData);
return $record;
}
/** @phpstan-ignore-next-line */
$relationship->save($record);
return $record;
});
$action->record($record);
$form->model($record)->saveRelationships();
$livewire->mountedTableActionRecord($record->getKey());

if ($arguments['another'] ?? false) {
$action->record(null);
// Ensure that the form record is anonymized so that relationships aren't loaded.
$form->model(self::class);
$livewire->mountedTableActionRecord(null);
$preservedData = Arr::only($data, ['property_id']);
$form->fill();
$form->fill($preservedData);
$action->successNotificationTitle('Comparable Unit added successfully');
$action->success();
$action->halt();
return;
}
$action->successNotificationTitle('Comparable Unit added successfully');
$action->success();
}
This works, but not my favorite solution as i have to include a lot of logic that im not overriding. If i can avoid it, please let me know. Im then calling it with:
->action(fn ($action, $form, $data, $livewire, $arguments) => ComparableUnit::createComparable($action, $form, $data, $livewire, $arguments))
->action(fn ($action, $form, $data, $livewire, $arguments) => ComparableUnit::createComparable($action, $form, $data, $livewire, $arguments))
ah shoot, this would require changes based on if this is a standalone or table action too. grrr There has to be a better way to do this. Seems pretty natural to want some customization with create another fills
dissto
dissto3mo ago
I needed something similar but also wasn't satisfied. I simply get the latest created model and fill it with that. This will inevitably introduce race conditions though. But in my case it was only for models that also have a user_id on it so it was no big deal🤔