This guide will have you adding custom fields to your Filament resources in minutes.
Step 1: Register the Plugin
In your AdminPanelProvider
or panel configuration, register the Custom Fields plugin:
use Relaticle\CustomFields\CustomFieldsPlugin;
use Filament\Panel;
public function panel(Panel $panel): Panel
{
return $panel
// ... other panel configurations
->plugins([
CustomFieldsPlugin::make(),
]);
}
Step 2: Prepare Your Model
Your model needs to implement the HasCustomFields
interface and use the UsesCustomFields
trait.
In your model (e.g., Product
), add the following:
use Relaticle\CustomFields\Models\Contracts\HasCustomFields;
use Relaticle\CustomFields\Models\Concerns\UsesCustomFields;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements HasCustomFields
{
use UsesCustomFields;
// ... your existing model code
}
To include custom fields in your resource forms, add the CustomFieldsComponent
to your form schema.
In your resource class (e.g., ProductResource
), modify the form()
method:
use Relaticle\CustomFields\Filament\Forms\Components\CustomFieldsComponent;
use Filament\Forms;
use Filament\Forms\Form;
class ProductResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
// Your existing form fields
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('price')
->numeric(),
// Add the CustomFieldsComponent
CustomFieldsComponent::make(),
]);
}
// ...
}
Step 4: Display Custom Fields in Tables
To display custom fields in your table views, add the InteractsWithCustomFields
trait to your resource’s ListRecords
page.
In your list records class (e.g., ListProducts
), add the trait:
use Relaticle\CustomFields\Filament\Tables\Concerns\InteractsWithCustomFields;
use Filament\Resources\Pages\ListRecords;
class ListProducts extends ListRecords
{
use InteractsWithCustomFields;
// ...
}
This trait automatically includes all custom fields in the table view for the resource.
Step 5: Add Custom Fields to Info Lists
In your resource’s ViewRecord
page, include the CustomFieldsInfolists
component:
use Relaticle\CustomFields\Filament\Infolists\CustomFieldsInfolists;
use Filament\Resources\Pages\ViewRecord;
use Filament\Infolists;
class ViewProduct extends ViewRecord
{
// ...
public function getInfolist(): array
{
return [
// ... existing infolist components
Infolists\Components\TextEntry::make('name')
->label('Product Name'),
Infolists\Components\TextEntry::make('price')
->label('Price'),
// Custom Fields
CustomFieldsInfolists::make()
->columnSpanFull(),
];
}
}
Step 6: Include Custom Fields in Exports (Optional)
To include custom fields in your exports, use the CustomFieldsExporter
component:
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Relaticle\CustomFields\Filament\Exports\CustomFieldsExporter;
class ProductExporter extends Exporter
{
protected static ?string $model = Product::class;
public static function getColumns(): array
{
return [
ExportColumn::make('id')
->label('ID'),
ExportColumn::make('name'),
ExportColumn::make('price'),
// Include custom fields
...CustomFieldsExporter::getColumns(self::getModel()),
];
}
}
That’s It!
You now have custom fields integrated into your Filament resource! Your users can:
- Add custom fields through the admin panel
- Fill out custom fields in forms
- View custom fields in tables and info lists
- Export custom fields data
Next Steps
Responses are generated using AI and may contain mistakes.