How to customize the filament creating and editing action

In this article, we are going to explore how we can customize the filament creating and edition action for our modification.

Lets take a simple example, you want to fill the user_id automatically when the user submits the form. If we will use select field to choose the current user or create a form field and set the default value to it, i think its not an appropriate way. Also, you can take an example of adding extra attributes on it. So, for that we can modify the case.

Usecase/ Scenario

So, for this article, I have a customer resource but it is same as users. The only difference among these two is type attribute on users table. So, for that , what i want is when i add an user, i want to add user type “customer” while saving.

Check out the official docs.

Modifying Creation Process

So, first we will check how we can modify the customization process of a record. For that we need to go to the Creating page of particular resource. From that page, we can override the following code:

Advertisements

  protected function handleRecordCreation(array $data): Model
    {
        $data['type'] = UserType::CUSTOMER;
        return static::getModel()::create($data);
    }

In the above handleRecordCreation() method, it has an array of form data and I have added type key on that $data variable. Remember, this method must always return model instance.

Modifying Editing Process

Similarly, we can also customize the editing process of a record. For that, we need to be on Editing page of a particular resource. From that page, we can override the following code:

protected function handleRecordUpdate(Model $record, array $data): Model
{
    $data['edited_by'] = auth()->id()
    $record->update($data);
    return $record;
}

In the same way, it should also return a model instance.

Check out more filament articles.

Modifying Creation Process on Modal Actions

Now, if the actions are based on modal not on resource, we can easily customize it. For that , we just need to chain the using() method on it. Lets take an example.

 
CreateAction::make()
    ->using(function (array $data, string $model): Model {
         $data['my_key'] = "my_value";
        return $model::create($data);
    })

Here you can see it has form data as well as the current model. And it also must return model instance.

Modifying Editing Process on Modal Actions

Similarly, if we want to modify the editing process, we need to chain using() method. Lets take an example:

EditAction::make()
    ->using(function (array $data, string $model): Model {
         $data['my_key'] = "my_value";
        return $model::create($data);
    })

Conclusion

In this way, we can customize the filament creating and editing action from both resources as well as modal actions. If you have any confusion, you can comment down below.

Advertisements

1 thought on “How to customize the filament creating and editing action”

  1. Pingback: How to Add a Custom ‘Add New’ Option in Dropdown with Auto Form Display - Code Daily

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top