F
Filament7mo ago
Randak

Editing a many-to-many attachment with pivot attributes.

I have a Creature and Shield model with a CreatureShield pivot table. The belongsToMany relationship is defined in both Models. I can attach and detach from the Creature model, including adding pivot table fields for new attachments. When I try to edit an existing attachment I get this error: Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in /var/www/html/vendor/filament/forms/src/Components/Select.php This is my first Laravel project and I'm a hobby coder so please excuse my lack of knowledge.
Solution:
I solved the problem by removing the "shield_id" select statement.
Jump to solution
2 Replies
Randak
RandakOP7mo ago
class ShieldsRelationManager extends RelationManager
{
protected static string $relationship = 'shields';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('shield_id')
->relationship('shield', 'description')
->required(),
Forms\Components\Select::make('combat_action')
->required()
->options([
'attack' => 'Attack',
'parry' => 'Parry',
]),
Forms\Components\TextInput::make('fame_points')
->required()
->integer(),
]);
}

public function table(Table $table): Table
{
return $table
->allowDuplicates()
->recordTitleAttribute('description')
->columns([
Tables\Columns\TextColumn::make('description')
->sortable(),
Tables\Columns\TextColumn::make('combat_action')
->sortable(),
Tables\Columns\TextColumn::make('fame_points')
->sortable(),

])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\Select::make('combat_action')
->options([
'attack' => 'Attack',
'parry' => 'Parry',
])
->required(),
Forms\Components\TextInput::make('fame_points')
->numeric()
->required(),
]),
])
class ShieldsRelationManager extends RelationManager
{
protected static string $relationship = 'shields';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('shield_id')
->relationship('shield', 'description')
->required(),
Forms\Components\Select::make('combat_action')
->required()
->options([
'attack' => 'Attack',
'parry' => 'Parry',
]),
Forms\Components\TextInput::make('fame_points')
->required()
->integer(),
]);
}

public function table(Table $table): Table
{
return $table
->allowDuplicates()
->recordTitleAttribute('description')
->columns([
Tables\Columns\TextColumn::make('description')
->sortable(),
Tables\Columns\TextColumn::make('combat_action')
->sortable(),
Tables\Columns\TextColumn::make('fame_points')
->sortable(),

])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\Select::make('combat_action')
->options([
'attack' => 'Attack',
'parry' => 'Parry',
])
->required(),
Forms\Components\TextInput::make('fame_points')
->numeric()
->required(),
]),
])
The closest I've got is adding
->multiple
->multiple
Select:make(shield_id) That allows me to open the edit form, but does not pre-populate the shield description. When I click on that field it brings up the search (no dropdown) and then errors when I type anything. The other fields seem to edit fine.
Solution
Randak
Randak7mo ago
I solved the problem by removing the "shield_id" select statement.
Want results from more Discord servers?
Add your server