Have you ever needed to reuse the same form fields across multiple resources in Filament? If yes, then how did you implement it?. In this guide, we will explore different techniques for reusing or extending filament form resources.
Check out the official docs for filament.
Introduction
So, first of all , lets took a look at the basic form resources for filament.
public static function form(Form $form): Form
{
return $form->schema([
TextInput::make('name')->required()
]);
}
Now lets see how we can extend and reuse these form effieciently.
1. Directly Extending the resources
If two or more of your resources are almost same except the queries, then we can directly extend the resources and modify according to our case. So, to illustrate this , lets suppose we have 2 things: Customer and Supplier. And the form resource of Customer and Supplier is same, then we can create one resource and extend it.
Advertisements
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()
->columns(2)
->schema(Forms\Components\TextInput::make("name")
->label(__("Name"))
->required(),
Forms\Components\TextInput::make("email")
->label(__("Email"))
->email(),
Forms\Components\TextInput::make("contact")
->label(__("Contact"))
->required(),
Forms\Components\TextInput::make("address")
->label(__("Address")),
Forms\Components\KeyValue::make("data")
->label(__("Extra Details")))
]);
}
So, if the same resource is used on our Supplier resource then we just extend this. So that is in supplier resource we can do:
class SupplierResource extends CustomerResource
{
// no need to call the form as it will automatically takes parent class form resource.
}
So, here we can modify the model and navigation property but leave the form method and then it will automatically takes parent form.
Check out more tips and tricks from filament.
2. Custom Form Component
Let’s suppose you have some form components that are used in multiple resources. So, what we can do is we can create a separate class and create a method that returns our required form fields.
class UserDetailsForm{
public static function make():array{
Section::make()
->schema([
TextInput::make('name')->required(),
TextInput::make('email')->required()
])
}
}
So, now this form schema can be used on any other forms if we need. So for that, we can just use the following way:
public static function form(Form $form): Form
{
return $form
->schema([
...UserDetailsForm::make(),
// other form components.
]);
}
3. Use Trait for Shared Fields
If multiple resources share the same fields, using a trait is a clean way to keep our code organized. We can just call the trait method and everything works like a magic.
trait HasUserFields
{
public static function userFields():array{
Section::make()
->schema([
TextInput::make('name')->required(),
TextInput::make('email')->required()
])
}
}
Now, we can easily use this trait on our form schema for extending form resources.
public static function form(Form $form): Form
{
return $form
->schema([
...self::userFields(),
// other form components.
]);
}
Trait are best to make the code organized and works on DRY (Do not Repeat yourself) principle.
4. Extending parent resource with more fields
As in first number, we discuss we can extend parent resource directly. But what if we want some customizable or to add more fields on that. For that we can easily do the following:
class SupplierResource extends CustomerResource
{
}public static function form(Form $form): Form
{
return $form
->schema([
...parent::form($form)->getComponents(),
// other form components.
]);
}
So, now we will get the parent schema as well as we can create our own schema for extending filament form resources.
Conclusion
These are the four ways, but you can still extend or reuse it on different ways. The only thing we need to remember is we should not repeat ourself. Its best to reuse the same components if they can be. If you have any queries or comments, please comment below or email me.
Pingback: How to deploy laravel project in cpanel using git? - Code Daily