Skip to main content

Extending Existing Resources

To extend an existing resource, create a new class that implements the resource contract:

1. Create the Contract

First, create a contract interface:
namespace App\Contracts\Admin\Resources;

use Filament\Resources\Resource;

interface ProductResourceContract
{
    // Define your custom resource interface
}

2. Create the Custom Resource

Create your custom resource class:
namespace App\Filament\Resources;

use Esign\UnleashCommerce\Admin\Filament\Resources\Products\ProductResource;
use Filament\Forms\Form;
use Filament\Tables\Table;

class CustomProductResource extends ProductResource
{
    public static function form(Form $form): Form
    {
        $form = parent::form($form);
        
        // Add your custom form fields here
        return $form;
    }

    public static function table(Table $table): Table
    {
        $table = parent::table($table);
        
        // Customize your table here
        return $table;
    }
}

Registering Custom Resources

Register your custom resource using the FilamentResourceManifest:
use Esign\UnleashCommerce\Admin\Support\FilamentResourceManifest;
use Esign\UnleashCommerce\Admin\Contracts\Filament\Resources\ProductResource as ProductResourceContract;

public function boot()
{
    $manifest = $this->app->make(FilamentResourceManifest::class);
    $manifest->replace(
        ProductResourceContract::class,
        CustomProductResource::class
    );
}

Creating Additional Form Fields

Extend a resource’s form with custom fields:
public static function form(Form $form): Form
{
    $form = parent::form($form);
    
    return $form->schema([
        ...parent::form($form)->getComponents()[0]->getComponents(),
        
        Section::make('Custom Fields')
            ->schema([
                TextInput::make('custom_sku')
                    ->label('Custom SKU'),
                Select::make('custom_category')
                    ->options(Category::pluck('name', 'id')),
            ]),
    ]);
}

Adding Custom Actions

Add custom actions to your resource:
use Filament\Actions\Action;

public static function table(Table $table): Table
{
    return $table
        ->actions([
            Action::make('export')
                ->label('Export')
                ->action(function ($record) {
                    return redirect()->to('/export/' . $record->getKey());
                }),
            Tables\Actions\EditAction::make(),
        ]);
}

Testing Custom Resources

Test your custom resources:
use Livewire\Livewire as Livewire;

public function test_custom_product_resource_displays()
{
    // Arrange
    $product = Product::factory()->create();
    
    // Act
    $response = $this->actingAs($this->admin)
        ->get("/admin/products/{$product->getKey()}");
    
    // Assert
    $response->assertSuccessful();
}
For detailed information about Filament Resources, see the Filament documentation.