The Custom Fields plugin provides robust import and export capabilities for handling custom field data. This documentation explains how to integrate custom fields with Filament’s import/export actions.

Import Implementation

Set Up the Importer

namespace App\Filament\Imports;

use App\Models\Product;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Facades\Filament;
use Relaticle\CustomFields\Filament\Imports\CustomFieldsImporter;

class ProductImporter extends Importer
{
    protected static ?string $model = Product::class;
    
    // Simplest implementation: Add columns in getColumns()
    public static function getColumns(): array
    {
        return [
            // Regular columns
            ImportColumn::make('name')->requiredMapping()->rules(['required']),
            ImportColumn::make('price')->numeric(),
            
            // Add all custom fields automatically
            ...app(CustomFieldsImporter::class)->getColumns(self::getModel()),
        ];
    }
    
    // Before filling model with data, remove custom fields
    protected function beforeFill(): void
    {
        $this->data = app(CustomFieldsImporter::class)->filterCustomFieldsFromData($this->data);
    }
    
    // After saving model, save custom fields
    protected function afterSave(): void
    {
        // Get the tenant if using multi-tenancy
        $tenant = Filament::getTenant();
        
        app(CustomFieldsImporter::class)->saveCustomFieldValues(
            $this->record,           // The model instance
            $this->getOriginalData(), // Original data with custom fields
            $tenant                  // Optional: tenant for multi-tenancy
        );
    }
}

Add Import Action to Your Resource

use Filament\Actions\ImportAction;
use App\Filament\Imports\ProductImporter;

// In your resource class
public static function getActions(): array
{
    return [
        ImportAction::make()
            ->importer(ProductImporter::class),
    ];
}

Selective Custom Fields

Only include specific custom fields:

// Import specific fields only
...app(CustomFieldsImporter::class)->getColumnsByFieldCodes(
    self::getModel(), 
    ['product_color', 'product_size']
),

Export Implementation

Set Up the Exporter

namespace App\Filament\Exports;

use App\Models\Product;
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;

    // Simply add your columns and spread in the custom fields
    public static function getColumns(): array
    {
        return [
            ExportColumn::make('id'),
            ExportColumn::make('name'),
            ExportColumn::make('price'),
            
            // Add all custom fields automatically
            ...CustomFieldsExporter::getColumns(self::getModel()),
        ];
    }
}

Add Export Action to Your Resource

use Filament\Actions\ExportAction;
use App\Filament\Exports\ProductExporter;

// In your resource class
public static function getActions(): array
{
    return [
        ExportAction::make()
            ->exporter(ProductExporter::class),
    ];
}