Repeater pivot

Hi, I'm having a problem with the repeater. My problem: I've created a pivot table but I don't understand why it doesn't go into the pivot table but into the artist table, so it creates a new artist for me every time I add it, when my aim is for it to register only in my pivot table.
Repeater::make('artists')
->required()
->relationship()
->label(__('lead-resource.repeater.field.artists'))
->schema([
Forms\Components\Select::make('artists')
->searchable()
->getSearchResultsUsing(fn(string $search): array =>
Artist::where('name', 'like', "{$search}%")
->orWhere('first_name', 'like', "{$search}%")
->orWhere('zip_code', 'like', "{$search}%")
->select('id', 'name', 'first_name', 'zip_code')
->limit(50)
->get()
->mapWithKeys(fn($artist) => [$artist->id => "{$artist->first_name} {$artist->name} - {$artist->zip_code}"])
->toArray())
->getOptionLabelUsing(function ($value): ?string {
$artist = Artist::find($value);
return "{$artist->first_name} {$artist->name} - {$artist->zip_code}";
})
->label(__('lead-resource.repeater.field.name'))
->columnSpan(2),
Repeater::make('artists')
->required()
->relationship()
->label(__('lead-resource.repeater.field.artists'))
->schema([
Forms\Components\Select::make('artists')
->searchable()
->getSearchResultsUsing(fn(string $search): array =>
Artist::where('name', 'like', "{$search}%")
->orWhere('first_name', 'like', "{$search}%")
->orWhere('zip_code', 'like', "{$search}%")
->select('id', 'name', 'first_name', 'zip_code')
->limit(50)
->get()
->mapWithKeys(fn($artist) => [$artist->id => "{$artist->first_name} {$artist->name} - {$artist->zip_code}"])
->toArray())
->getOptionLabelUsing(function ($value): ?string {
$artist = Artist::find($value);
return "{$artist->first_name} {$artist->name} - {$artist->zip_code}";
})
->label(__('lead-resource.repeater.field.name'))
->columnSpan(2),
Solution:
Did you create a hasMany relationship with the pivot class? Also, there are certain limitations with repeaters - you need a unique ID on the pivot table for them to work property.
Please ensure that your pivot model has a primary key column, like id, to allow Filament to keep track of which repeater items have been created, updated and deleted.
https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-a-belongstomany-eloquent-relationship...
Jump to solution
4 Replies
Lara Zeus
Lara Zeus16mo ago
didnt test your code, but colud it be coz you have the same name artists in the Repeater and the select?
Becker Maxime
Becker MaximeOP16mo ago
I changed the name and the problem persists.
Solution
ChesterS
ChesterS16mo ago
Did you create a hasMany relationship with the pivot class? Also, there are certain limitations with repeaters - you need a unique ID on the pivot table for them to work property.
Please ensure that your pivot model has a primary key column, like id, to allow Filament to keep track of which repeater items have been created, updated and deleted.
https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-a-belongstomany-eloquent-relationship
Becker Maxime
Becker MaximeOP16mo ago
Thank you, you saved my life, I lost 4 hours, I could have lost much more. bisous 🙂

Did you find this page helpful?