This section will guide you through setting up and using the Custom Fields plugin in your Filament admin panel.

Publish and Run Migrations

Publish the migration files:

php artisan vendor:publish --tag="custom-fields-migrations"

Run the migrations:

php artisan migrate

Publish the Configuration File (Optional)

If you wish to customize the configuration, publish the config file:

php artisan vendor:publish --tag="custom-fields-config"

This will create a custom-fields.php file in your config directory.

Integrating Custom Fields Plugin into panel

To use Custom Fields, you need to register the plugin with your Filament panel.

use Relaticle\CustomFields\CustomFieldsPlugin;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ... other panel configurations
        ->plugins([
            CustomFieldsPlugin::make(),
        ]);
}

Setting Up the Model

Your model needs to implement the HasCustomFields interface and use the UsesCustomFields trait to work with custom fields.

In your model (e.g., Company), add the following:

use Relaticle\CustomFields\Models\Contracts\HasCustomFields;
use Relaticle\CustomFields\Models\Concerns\UsesCustomFields;
use Illuminate\Database\Eloquent\Model;

class Company extends Model implements HasCustomFields
{
    use UsesCustomFields;

    // ... your existing model code
}

By following these steps, your model will be fully equipped to handle custom fields, and you will have successfully set up Custom Fields in your Filament application, enabling dynamic and flexible data management.

Adding Custom Fields to Forms

To include custom fields in your resource forms, you’ll use the CustomFieldsComponent. This component dynamically adds all the custom fields associated with the resource.

In your resource class (e.g., CompanyResource), modify the form() method:

use Relaticle\CustomFields\Filament\Forms\Components\CustomFieldsComponent;
use Filament\Forms;
use Filament\Resources\Form;

class CompanyResource 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('email')
                    ->email(),

                // Add the CustomFieldsComponent
                CustomFieldsComponent::make()
                    ->columns(1),
            ]);
    }

    // ...
}

Displaying Custom Fields in Table Views

To display custom fields in your table views, you need to use the InteractsWithCustomFields trait in your resource’s ListRecords page.

In your list records class (e.g., ListCompanies), add the trait:

use Relaticle\CustomFields\Filament\Tables\Concerns\InteractsWithCustomFields;
use Filament\Resources\Pages\ListRecords;

class ListCompanies extends ListRecords
{
    use InteractsWithCustomFields;

    // ...
}

This trait automatically includes all custom fields in the table view for the resource.

Integrating into Info Lists

In your resource’s ViewRecord page or wherever you define the info list schema, include the CustomFieldsInfolists component:

use Relaticle\CustomFields\Filament\Infolists\CustomFieldsInfolists;
use Filament\Resources\Pages\ViewRecord;

class ViewCompany extends ViewRecord
{
    // ...

    public function getInfolist(): array
    {
        return [
            // ... existing infolist components
            Infolists\Components\TextEntry::make('name')
                ->label('Company Name'),
            Infolists\Components\TextEntry::make('email')
                ->label('Email Address'),
    
            // Custom Fields
            CustomFieldsInfolists::make()
                ->columnSpanFull(),
        ];
    }
}

Including Custom Fields in Exports

To include custom fields in your exports, you can leverage the CustomFieldsExporter component, which is designed to integrate seamlessly with Filament’s exporting tools. Below is your CompanyExporter class, which shows how to implement this:

use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Relaticle\CustomFields\Filament\Exports\CustomFieldsExporter;

class CompanyExporter extends Exporter
{
    protected static ?string $model = Comapny::class;

    public static function getColumns(): array
    {
        return [
            ExportColumn::make('id')
                ->label('ID'),
            ExportColumn::make('name'),
            ExportColumn::make('email'),

            // Include custom fields
            ...CustomFieldsExporter::getColumns(self::getModel()),
        ];
    }
}