Many to Many and Filament Roles and the Users Table - dealing with Undefined array key "recordId"
FilamentWhen working with Filament PHP, it's somewhat easy to set up many to many roles. The pertinent place in the manual is here:
https://filamentphp.com/docs/3.x/panels/resources/relation-managers
At times, when you have a more complex User with a number of roles, things can really get tricky. All of a sudden the "out of the box" things don't work anymore. However, reading carefully through the documentation often has the clue. Only problem of course, the documentation page is really long, and complex.
The key tip is repeated 4 times on the page:
Please ensure that any pivot attributes are listed in the withPivot() method of the relationship and inverse relationship.
There is a 5th variation:
Please ensure that the id attribute is listed in the withPivot() method of the relationship and inverse relationship.
Note to self: Make sure the pivot attributes and listed on both the forward and reverse relationship.
If you don't follow this advice, you will see the strangest behavior.
The next challenge is this error message:
Undefined array key "recordId"
Let's try help, heck, let's try "Ask" too:
->headerActions([ Tables\Actions\AttachAction::make() ->form(fn (AttachAction $action): array => [ $action->getRecordSelect(), Forms\Components\TextInput::make('amount')->required(), // Forms\Components\TextInput::make('role')->required(), ]) // ->preloadRecordSelect() // ->form([ // // Select the rate // Forms\Components\Select::make('rate_id') // ->options(Rate::all()->pluck('name', 'id')), // // Set the amount // Forms\Components\TextInput::make('amount') // ->required() // ->numeric() // ->minValue(0), // ]), // Tables\Actions\CreateAction::make(), ])
I've intentionally left commented code to give you some idea of the struggles of getting this right.