F
Filament5mo ago
Hasith

Insert data to a pivot table in table CreateAction?

I have Gem Model and Gem has many Sizes and each size have a price. Size can be belongs to many gems. So i create and pivot table. I added a repeater to set the sizes and price. But Gem creation gives me an error. this is my action and form ===>
Repeater::make('sizes')
->relationship('sizes')
->schema([
Select::make('size_id')
->label('Size')
->options(Size::all()->pluck('size', 'id'))
->searchable()->preload()
->required()->disableOptionsWhenSelectedInSiblingRepeaterItems(),
TextInput::make('price')->suffix('$')->required()->numeric()
->inputMode('decimal'),
])->columns(2)->columnSpanFull()->minItems(1),


public function sizes(): BelongsToMany
{
return $this->belongsToMany(Size::class, 'gem_sizes', 'gem_id', 'size_id')->withPivot('price')->withTimestamps();
}
Repeater::make('sizes')
->relationship('sizes')
->schema([
Select::make('size_id')
->label('Size')
->options(Size::all()->pluck('size', 'id'))
->searchable()->preload()
->required()->disableOptionsWhenSelectedInSiblingRepeaterItems(),
TextInput::make('price')->suffix('$')->required()->numeric()
->inputMode('decimal'),
])->columns(2)->columnSpanFull()->minItems(1),


public function sizes(): BelongsToMany
{
return $this->belongsToMany(Size::class, 'gem_sizes', 'gem_id', 'size_id')->withPivot('price')->withTimestamps();
}
1 Reply
Hasith
Hasith5mo ago
No description