F
Filament2y ago
Remi

Repeater BelongsToMany not working on edit form

i followed the steps from the docs: https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-a-belongstomany-eloquent-relationship This works on creation, but on the edit form it only shows a single record, not all the items. On the Client Resource:
Forms\Components\Repeater::make('clientLocations')
->relationship()
->simple(
Forms\Components\Select::make('location_id')
->relationship('location', 'name')
->required()
)
->columns(1),
Forms\Components\Repeater::make('clientLocations')
->relationship()
->simple(
Forms\Components\Select::make('location_id')
->relationship('location', 'name')
->required()
)
->columns(1),
ClientLocation Pivot model has:
public function client(): BelongsTo
{
return $this->belongsTo(Client::class);
}

public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}
public function client(): BelongsTo
{
return $this->belongsTo(Client::class);
}

public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}
Also when I submit the edit page and change the single record it shows, it gives an error:
update
`client_location`
SET
`location_id` = 3
WHERE
`` = 39
AND `` = 39
update
`client_location`
SET
`location_id` = 3
WHERE
`` = 39
AND `` = 39
5 Replies
Remi
RemiOP2y ago
For future reference: My newly created Pivot Model didnt have an ID column. This is required. So if you run in to this problem and your pivot table doesnt have an id column. Add it ❤️
KeyMe
KeyMe15mo ago
adding it where exactly? i though id exists automatically,
KA
KA15mo ago
What is your table structure?
KeyMe
KeyMe15mo ago
Order table, Device table, custom DeviceOrder pivot model with id, order_id, device_id, quantity.
KA
KA15mo ago
you should use BelongsToMany relationship i think

Did you find this page helpful?