© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
5 replies
whimsical_pomelo_17483

BelongsToMany relation issue

Hi

I have issue to setup the relationManager for belongsToMany relations with pivot table.

I have 2 tables and a pivot table with ID & TABLE1_ID et TABLE2_ID. Both eloquent model have the related relation set with the return belongsToMany to the other table.

I have created a php artisan make:filament-relation-manager and add the class in the getRelations() methods.

On the frontend in the edit form of TABLE1, the HTML table listing all the rows from the PIVOT table is showing nicely. If i change directly in mySql a TABLE2_ID in a row of the pivot table, it reflects fine on the html table after refresh, nice !

Now the issue :

When using the create button, it show a form a when i save the data, it crashes because it tries to insert in the TABLE2 table without some required field.

It shows only a CREATE button on the related HTML table, no ATTACH.

I do not want to have the possibility to add directly a TABLE2 row from this form, i have a separate resource for managing TABLE2.

I only want to be able to add / edit a row of the PIVOT table and some extra columns beside the 2 IDs in this table.

Any idea on what to check ?

Thx !
Solution
In the table() definition in your RM, remove the CreateAction and add an AttachAction, with a form() to add any additional pivot attributes. Here's an example from one of my apps where I'm adding a role_id pivot attribute.

 public function table(Table $table): Table
 {
     return $table
         ->columns([
           //
         ])
         ->headerActions([
             Tables\Actions\AttachAction::make()
                 ->form(fn (Tables\Actions\AttachAction $action): array => [
                     $action->getRecordSelect(),
                     Select::make('role_id')
                         ->options(Role::all()->pluck('name', 'id'))
                         ->required(),
                 ])
          ])
          //
}
 public function table(Table $table): Table
 {
     return $table
         ->columns([
           //
         ])
         ->headerActions([
             Tables\Actions\AttachAction::make()
                 ->form(fn (Tables\Actions\AttachAction $action): array => [
                     $action->getRecordSelect(),
                     Select::make('role_id')
                         ->options(Role::all()->pluck('name', 'id'))
                         ->required(),
                 ])
          ])
          //
}


As per the docs, make sure any pivot attributes you use in the form are included in the relationship on your model with withPivot.

https://filamentphp.com/docs/3.x/panels/resources/relation-managers#attaching-with-pivot-attributes
Managing relationships - Panel Builder - Filament
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Dependant select with BelongsToMany relation
FilamentFFilament / ❓┊help
3y ago
Adding BelongsToMany that includes another BelongsToMany relation to a form
FilamentFFilament / ❓┊help
3y ago
belongsToMany select for relation not working
FilamentFFilament / ❓┊help
6mo ago
Using Select with a BelongsToMany relation
FilamentFFilament / ❓┊help
3y ago