F
Filament2mo ago
Lovins

Open relation manager on table action

Is there a way to open a relationship management modal directly in the filament table? Without having to click edit the record and click add relationship.
3 Replies
toeknee
toeknee2mo ago
So you have a reousrce which shows a table list, then theres an edit window and the relationship manager is in the edit? If so... you should be able to create your own action, on the record say' Add My Relationship' which uses the form from the relationship manager, then in the ->action() save the data accordingly.
Lovins
LovinsOP2mo ago
That would be it, but I didn't find a way to do it in the filament documentation, I tried with the gpt chat too but it gave me solutions that didn't work
toeknee
toeknee2mo ago
Don't use chatGPT it's not relavent really. Two mins, i'll writ eoyu a base In your ->actions([]) add
\Filament\Tables\Actions\Action::make('my_relation_action')
->label('Relation Records')
->hiddenLabel()
->modalHeading(fn ($record) => 'Editing Details For: '.$record->first_name.' '.$record->last_name)
->model(fn ($record) => $record->accounts) // The relationship
->fillForm(fn ($record) => ['accounts' => $record->accounts->toArray()])
->form([
Repeater::make('accounts')
->relationship('bank_accounts')
->defaultItems(1)
->schema(
MyREsourceClass::customFormSchemaFunction() // Returns an array from a public static function of the TextInputs etc.
)
->columnSpanFull()
->columns(2)
->addActionLabel('Add Account')
->label(fn ($record) => ' Account Details'),
])
->slideOver()
->action(function ($record, $data) {
Notification::make()
->title('Accounts Updated Successfully')
->icon('heroicon-o-document-text')
->iconColor('success')
->send()
->sendToDatabase(auth()->user());
}),
\Filament\Tables\Actions\Action::make('my_relation_action')
->label('Relation Records')
->hiddenLabel()
->modalHeading(fn ($record) => 'Editing Details For: '.$record->first_name.' '.$record->last_name)
->model(fn ($record) => $record->accounts) // The relationship
->fillForm(fn ($record) => ['accounts' => $record->accounts->toArray()])
->form([
Repeater::make('accounts')
->relationship('bank_accounts')
->defaultItems(1)
->schema(
MyREsourceClass::customFormSchemaFunction() // Returns an array from a public static function of the TextInputs etc.
)
->columnSpanFull()
->columns(2)
->addActionLabel('Add Account')
->label(fn ($record) => ' Account Details'),
])
->slideOver()
->action(function ($record, $data) {
Notification::make()
->title('Accounts Updated Successfully')
->icon('heroicon-o-document-text')
->iconColor('success')
->send()
->sendToDatabase(auth()->user());
}),
Very rough base This is for a hasMany relationship but gives you the idea

Did you find this page helpful?