In Filament v3, the “Create and Create Another” feature re-renders the form blank after saving by default. If you want to pre-populate the new record form with a subset of the data just saved, you need to override the behavior after the record is created.
Here’s how you can approach this:
- Override the
mutateFormDataBeforeCreateor define your own method to copy over the attributes you need. - Use the
afterCreateorafterCreateAnotherhook to modify the form's state for the next item.
Let’s say you have a PostResource and want to copy, for example, the category_id to the new form each time you create another.
use Filament\Resources\Pages\CreateRecord;
class CreatePost extends CreateRecord
{
protected function afterCreate(): void
{
// Save the fields you want to retain in the session or a property.
session([
'post.last_category_id' => $this->record->category_id,
]);
}
protected function getFormModel(): string
{
return \App\Models\Post::class;
}
protected function mutateFormDataBeforeFill(array $data): array
{
// When the form is freshly loaded (after "Create Another"),
// pre-fill the field(s) from the session if available.
if (session()->has('post.last_category_id')) {
$data['category_id'] = session('post.last_category_id');
}
return $data;
}
}
Notes:
- The
afterCreatehook will run after each record is created. You can use it to store the data you want to persist (for the next create). - The
mutateFormDataBeforeFillhook runs when the form is (re)filled, which happens when “Create Another” is used. Here, you inject the data saved from the previous record. - You may want to clear the session key when leaving the page or after the normal “Create” flow.
Alternative:
If you need more dynamic or complex behavior, you can also use form state management methods and properties to achieve a similar result.
Let me know if you need an example for a specific scenario or a different resource!