Introduction
Filament includes an action that is able to create Eloquent records. When the trigger button is clicked, a modal will open with a form inside. The user fills the form, and that data is validated and saved into the database. You may use it like so:Customizing data before saving
Sometimes, you may wish to modify form data before it is finally saved to the database. To do this, you may use themutateDataUsing() method, which has access to the $data as an array, and returns the modified version:
Customizing the creation process
You can tweak how the record is created with theusing() method:
$model is the class name of the model, but you can replace this with your own hard-coded class if you wish.
Redirecting after creation
You may set up a custom redirect when the form is submitted using thesuccessRedirectUrl() method:
$record parameter:
Customizing the save notification
When the record is successfully created, a notification is dispatched to the user, which indicates the success of their action. To customize the title of this notification, use thesuccessNotificationTitle() method:
successNotification() method:
successNotification(null) method:
Lifecycle hooks
Hooks may be used to execute code at various points within the action’s lifecycle, like before a form is saved. There are several available hooks:Halting the creation process
At any time, you may call$action->halt() from inside a lifecycle hook or mutation method, which will halt the entire creation process:
cancel() the action instead of halting it:
Using a wizard
You may easily transform the creation process into a multistep wizard. Instead of using aschema(), define a steps() array and pass your Step objects:
skippableSteps() method:
Creating another record
Modifying the create another action
If you’d like to modify the “create another” action, you may use thecreateAnotherAction() method, passing a function that returns an action. All methods that are available to customize action trigger buttons can be used:
Disabling create another
If you’d like to remove the “create another” button from the modal, you can use thecreateAnother(false) method:
Preserving data when creating another
By default, when the user uses the “create and create another” feature, all the form data is cleared so the user can start fresh. If you’d like to preserve some of the data in the form, you may use thepreserveFormDataWhenCreatingAnother() method, passing an array of fields to preserve:
$data to preserve:
$data array: