BelongsToMany records are not shown in Repeater

Hey 👋 I'd like to use a Repeater to present a n:m relationship. As I found in the documentation I refactored my code and introduced a Pivot model. Now I tried the Repeater for the first time and facing following issue: After I hit save, the records are persisted within the database. When I reload the admin panel, no records are shown in my Repeater.
No description
No description
25 Replies
alexanderkroneis
alexanderkroneisOP2y ago
This is the corresponding code:
Patrick Boivin
Did you change the relationship to HasMany, with the pivot model?
alexanderkroneis
alexanderkroneisOP2y ago
Yes, I did still facing this issue :/
alexanderkroneis
alexanderkroneisOP2y ago
When I hit Save in the form it's persisted. But why is it not displayed? 💀
No description
Patrick Boivin
Something to try, just to see if anything gets loaded into the field:
Repeater::make(...)
->afterStateHydrated(function ($state, $livewire) {
dd($state, $livewire->data);
})
Repeater::make(...)
->afterStateHydrated(function ($state, $livewire) {
dd($state, $livewire->data);
})
alexanderkroneis
alexanderkroneisOP2y ago
that looks fine
No description
alexanderkroneis
alexanderkroneisOP2y ago
No description
alexanderkroneis
alexanderkroneisOP2y ago
No console errors btw
Patrick Boivin
I see you have 2 nested repeaters, and both are using ->relationship(). Could this be the issue? The 1st level relationship is loading but the 2nd level isn't?
alexanderkroneis
alexanderkroneisOP2y ago
I use the same pattern at another place as well but there it's 1:n instead of n:m.
Patrick Boivin
Can you share your Room and Season models? To understand the relationships a bit better...
alexanderkroneis
alexanderkroneisOP2y ago
Sure, give me a moment
class Room extends Model
{
public function contracts(): BelongsToMany
{
return $this->belongsToMany(Contract::class);
}

public function seasons(): HasMany
{
return $this->hasMany(RoomSeason::class);
}
}
class Room extends Model
{
public function contracts(): BelongsToMany
{
return $this->belongsToMany(Contract::class);
}

public function seasons(): HasMany
{
return $this->hasMany(RoomSeason::class);
}
}
class RoomSeason extends Pivot
{
public function room(): BelongsTo
{
return $this->belongsTo(Room::class);
}

public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
}
class RoomSeason extends Pivot
{
public function room(): BelongsTo
{
return $this->belongsTo(Room::class);
}

public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
}
class Season extends Model
{
public function rooms(): HasMany
{
return $this->hasMany(RoomSeason::class);
}
}
class Season extends Model
{
public function rooms(): HasMany
{
return $this->hasMany(RoomSeason::class);
}
}
I removed unneccessary variables and methods.
ChesterS
ChesterS2y ago
FWIW I'm having the same (or a very similar) issue. Here's a repo with minimal code to reproduce it https://github.com/sprtk-ches/laravel-filament Another thing I notice is that some relationships are saved in the form->validate() step. Not sure if this is intentional but it looks weird and I'm getting the following error
update `user_roles` set `is_active` = 0
where `` = 9a235b23-9020-4e48-a980-e170c1d82929 and `` = 9a235b23-9020-4e48-a980-e170c1d82929
update `user_roles` set `is_active` = 0
where `` = 9a235b23-9020-4e48-a980-e170c1d82929 and `` = 9a235b23-9020-4e48-a980-e170c1d82929
Note that it doesn't pick up the column names in the where/and clause
alexanderkroneis
alexanderkroneisOP2y ago
@pboivin should we get someone from the core team into this? 🤔
Patrick Boivin
Probably... I think it's a bit beyond my depth. The only thing I can see is that the relationship to the pivot model is still seasons. Not sure if Filament relies on that name internally. Have you tried roomSeasons, to match the pivot? Not sure if anyone is available at the minute but I'll try to tag someone in.
alexanderkroneis
alexanderkroneisOP2y ago
I did not, let me try that first before tagging the team. No that did not help :/
Forms\Components\Tabs\Tab::make(__('Preise'))->schema([
Forms\Components\Repeater::make('rooms')
->label(__('Zimmer'))
->relationship('rooms')
->itemLabel(fn ($state) => $state['name'])
->addable(false)
->deletable(false)
->defaultItems(0)
->schema([
Forms\Components\Repeater::make('roomSeasons')
->label(__('Bepreisung'))
->addActionLabel(__('Preis hinzufügen'))
->relationship('roomSeasons')
->schema([...]),
]),
]),
Forms\Components\Tabs\Tab::make(__('Preise'))->schema([
Forms\Components\Repeater::make('rooms')
->label(__('Zimmer'))
->relationship('rooms')
->itemLabel(fn ($state) => $state['name'])
->addable(false)
->deletable(false)
->defaultItems(0)
->schema([
Forms\Components\Repeater::make('roomSeasons')
->label(__('Bepreisung'))
->addActionLabel(__('Preis hinzufügen'))
->relationship('roomSeasons')
->schema([...]),
]),
]),
that's the current form
awcodes
awcodes2y ago
I could be wrong but I’m fairly sure the name of the repeater shouldn’t be the same name as the relationship.
alexanderkroneis
alexanderkroneisOP2y ago
I'll check, give me a moment. You. are. fucking. genious. 🫶
alexanderkroneis
alexanderkroneisOP2y ago
I wanted to record it so you can see the behaviour and it worked after renaming the repeater. 😂
cheesegrits
cheesegrits2y ago
That is weird, because Filament will use the Repeater name as the relationship name, if you don't specify the relationship name. https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-an-eloquent-relationship
awcodes
awcodes2y ago
Yea. I think OP was creating a double nesting though. In this case.
alexanderkroneis
alexanderkroneisOP2y ago
Maybe there‘s an unknown issue when using nested Repeaters with relationship name and name being the same when the relation is an n:m 😂
awcodes
awcodes2y ago
Maybe.
alexanderkroneis
alexanderkroneisOP2y ago
I think I'll try to reproduce in a few days and if it occurs in a fresh repository as well, I'll open a issue on GitHub.

Did you find this page helpful?