Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaidpirwani's avatar

Re-use EDIT Action Form for Attach Action in Filament Resouce RelationManager

I am new to Laravel, Filament - but have good grasp on concepts and general coding. I have a Relation Manager on a model, ProcurementRequest for the relation items (Item Model)

I have created the basic form, but I need to write the same code twice - for EDIT Action and also for Attach Action - how can I use a single form for both ?

Any way I can create thsi form once and then call via a function ?

Code below: ItemsRelationManager.php

namespace App\Filament\Resources\ProcurementRequestResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Actions\AttachAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class ItemsRelationManager extends RelationManager
{
    protected static string $relationship = 'items';
    protected bool $allowsDuplicates = true;

        public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextArea::make('description')
                    ->required(),
                Forms\Components\TextInput::make('quantity')
                    ->required()
                    ->numeric()
                    ->live(onBlur: true)
                    ->afterStateUpdated(fn (Set $set, $state, Get $get) => $set('amount', $state *  $get('rate'))),
                Forms\Components\TextInput::make('rate')
                    ->required()
                    ->numeric()
                    ->live(onBlur: true)
                    ->afterStateUpdated(fn (Set $set, $state, Get $get) => $set('amount', $state *  $get('quantity'))),
                Forms\Components\TextInput::make('amount')
                    ->disabled()
                ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('name')
            ->allowDuplicates()
            ->columns([
                Tables\Columns\TextColumn::make('name'),
                Tables\Columns\TextColumn::make('description'),
                Tables\Columns\TextColumn::make('quantity'),
                Tables\Columns\TextColumn::make('rate'),
                Tables\Columns\TextColumn::make('amount'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\AttachAction::make()
                    ->preloadRecordSelect()
                    ->form(fn (AttachAction $action): array => [
                        $action->getRecordSelect(),
                        Forms\Components\TextArea::make('description')
                            ->required(),
                        Forms\Components\TextInput::make('quantity')
                            ->required()
                            ->numeric()
                            ->live(onBlur: true)
                            ->afterStateUpdated(fn (Set $set, $state, Get $get) => $set('amount', $state *  $get('rate'))),
                        Forms\Components\TextInput::make('rate')
                            ->required()
                            ->numeric()
                            ->live(onBlur: true)
                            ->afterStateUpdated(fn (Set $set, $state, Get $get) => $set('amount', $state *  $get('quantity'))),
                        Forms\Components\TextInput::make('amount')
                            ->disabled()
                ]),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DetachAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DetachBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
            ]);
    }
}
0 likes
1 reply
Grepy's avatar
Grepy
Best Answer
Level 1

Create form in separate class (extending Filament\Resources\Form) and then in your manager's form function just call return YourFormClass::make();

2 likes

Please or to participate in this conversation.