F
Filament2mo ago
Calden

Repeater ->relationship() inside multiple Group ->relationship() in a form issue

Hi! I’m having an issue with relationships in my form. I have a resource with a form based on my Inscription model. In this model, I have one student belongTo(Student::class) relationship. In my Student model, I have two relationships: AAAs → hasMany(AAA::class) BBBs → hasMany(BBB::class) In my form, I have fields like this:
/* NOT OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
...
/* NOT OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
...
and later:
/* OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("BBBs")
->relationship()
...
/* OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("BBBs")
->relationship()
...
The BBBs repeater displays its data correctly, but the AAAs repeater shows nothing. However, when I move the AAAs repeater into the same Group as the BBBs one, it works:
/* OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
...
Repeater::make("BBBs")
->relationship()
...
/* OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
...
Repeater::make("BBBs")
->relationship()
...
I don’t understand why I can’t have them in separate groups. if i move AAAs repeater Group after BBBs repeater Group, AAAs is working but BBBs dont anymore ...
/* NOT OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("BBBs")
->relationship()
...

/* OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
...
/* NOT OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("BBBs")
->relationship()
...

/* OK */
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
...
If anyone has an idea...
5 Replies
Dennis Koch
Dennis Koch2mo ago
You can try giving both Groups a unique key Group::make()->key('student_aaa'), but it might be the saving logic that only allows one Group with the same relation.
Calden
CaldenOP2mo ago
nop ->key doesnt work. And it dont seem to be on the saving logic cause when i add something in my AAAs, repeater, its well saved in the database, but not displayed on the form, only the last Group::make->relationship('student') display the repeater records well ... when i add a mutateRelationshipDataBeforeFillUsing on my buggy repeater, i can see my datas, but the repeater doesnt fill ...
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
->schema([
...
])
->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
dd($data);
return $data;
})
Group::make()
->relationship("student")
->schema([
Repeater::make("AAAs")
->relationship()
->schema([
...
])
->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
dd($data);
return $data;
})
borg
borg2mo ago
Sorry, but what element is this? A Group element having a relationship method?
Calden
CaldenOP2mo ago
I found a way to work around the issue by adding a ->mutateRelationshipDataBeforeFillUsing() on the last Group::make()->relationship('student') in my form to fill the previous Group::make()->relationship('student') blocks. But it feels a bit weird... Am I trying to do something too complicated? Was it not meant to be used that way? Are we only supposed to have only one Group::make()->relationship('') for the same relationship? Another thing I hadn’t mentioned: each group is in a different tab.
Tabs::make("Tabs")
->tabs([
Tab::make("stuffs1")
->schema([
/* Now filled by last Group ->mutateRelationshipDataBeforeFillUsing */
Group::make()
->relationship("student")
->schema([
Repeater::make("handicaps")
->relationship()
->schema([
...
])
]),
Tab::make("stuffs2")
->schema([
Group::make()
->relationship("student")
->schema([
Repeater::make("BBBs")
->relationship()
->schema([
...
])
])
->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
$record = Student::find($data['id']);

$handicaps = $record->handicaps->mapWithKeys(function ($handicap) {
return ['record-' . $handicap->id => [
'handicap_id' => $handicap->handicap_id,
'precision' => $handicap->precision,
]];
})->toArray();

$data['handicaps'] = $handicaps;

return $data;
})
])
])
Tabs::make("Tabs")
->tabs([
Tab::make("stuffs1")
->schema([
/* Now filled by last Group ->mutateRelationshipDataBeforeFillUsing */
Group::make()
->relationship("student")
->schema([
Repeater::make("handicaps")
->relationship()
->schema([
...
])
]),
Tab::make("stuffs2")
->schema([
Group::make()
->relationship("student")
->schema([
Repeater::make("BBBs")
->relationship()
->schema([
...
])
])
->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
$record = Student::find($data['id']);

$handicaps = $record->handicaps->mapWithKeys(function ($handicap) {
return ['record-' . $handicap->id => [
'handicap_id' => $handicap->handicap_id,
'precision' => $handicap->precision,
]];
})->toArray();

$data['handicaps'] = $handicaps;

return $data;
})
])
])

Did you find this page helpful?