Repeater in repeater eager load

It's possible to eager load nested relationship, like Question hasMany Answers and Answer HasMany Tags and all load in editView like :
// .......
Repeater::make('answers')
->relationship()
->schema([
Repeater::make('tags')
->relationship()
->schema([
TextInput::make('name'),
])
// ......
// .......
Repeater::make('answers')
->relationship()
->schema([
Repeater::make('tags')
->relationship()
->schema([
TextInput::make('name'),
])
// ......
If I modify
return parent::getEloquentQuery()->with('answers.tags');
return parent::getEloquentQuery()->with('answers.tags');
its loads answers and tags in eagerload and still for each answers query's tags and total is 309 selects..
17 Replies
Dan Harrin
Dan Harrin2y ago
is this in a resource?
AlexAnder
AlexAnderOP2y ago
Yep, in QuestionResource
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Group::make()
->schema([
Forms\Components\Group::make()
->schema(static::getFormSchema('answers'))
->columns(5),
])
->columnSpan(5),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Group::make()
->schema([
Forms\Components\Group::make()
->schema(static::getFormSchema('answers'))
->columns(5),
])
->columnSpan(5),
]);
}
public static function getFormSchema(?string $section = null): array
{
if ($section === 'answers') {
return [
Repeater::make('answers')
->relationship()
->schema([
Repeater::make('tags')
->relationship()
->schema([
TextInput::make('name'),
])
];
}
public static function getFormSchema(?string $section = null): array
{
if ($section === 'answers') {
return [
Repeater::make('answers')
->relationship()
->schema([
Repeater::make('tags')
->relationship()
->schema([
TextInput::make('name'),
])
];
}
Dan Harrin
Dan Harrin2y ago
maybe in beforeFill() on the edit page, load the relationships on $this->record
AlexAnder
AlexAnderOP2y ago
protected function mutateFormDataBeforeFill(array $data): array
{
$data['tags'] = $this->record->load('answers.tags');

return $data;
}
protected function mutateFormDataBeforeFill(array $data): array
{
$data['tags'] = $this->record->load('answers.tags');

return $data;
}
You mean this? +2 selects Or how to use
$data['tags']
$data['tags']
in repeater?
Dan Harrin
Dan Harrin2y ago
nope
protected function beforeFill(): void
{
$this->record->load('answers.tags');
}
protected function beforeFill(): void
{
$this->record->load('answers.tags');
}
AlexAnder
AlexAnderOP2y ago
Hmm. nope, the same + 2 selects..
Dan Harrin
Dan Harrin2y ago
im not sure then
AlexAnder
AlexAnderOP2y ago
Can I make custom page and with blade syntax made my own layout with nested relationships, then, probably save data also need to customize, right? Ah, so many non standart filament fetures I need.. 😦
Hro
Hro2y ago
I today ran into some performance issues when using nested repeaters. Tried to eager load in the model above but that did not change anything. Will dig a bit to see if I can make it work.
gustavo.dev
gustavo.dev17mo ago
FileUpload
John Wink
John Wink13mo ago
I also have the problem and have not yet found a solution. Eager Loading only works in the first level. With the 2+ nested repeater, it reloads every single Eloquent model.
John Wink
John Wink13mo ago
No matter whether I adjust the getEloquentQuery within the $with model or within the resource, it does nothing. It even loads more queries
No description
John Wink
John Wink13mo ago
Can someone with a clue take a look at this? I have customised the getCachedExistingRecords() function in the repeater component:
if(!is_null($relationship->getParent()->{$this->statePath})){
return $this->cachedExistingRecords = $relationship->getParent()->{$this->statePath}->mapWithKeys(
fn (Model $item): array => ["record-{$item[$relatedKeyName]}" => $item],
);
}
if(!is_null($relationship->getParent()->{$this->statePath})){
return $this->cachedExistingRecords = $relationship->getParent()->{$this->statePath}->mapWithKeys(
fn (Model $item): array => ["record-{$item[$relatedKeyName]}" => $item],
);
}
As a result, I have less than 50 queries instead of several 100
No description
John Wink
John Wink13mo ago
Just tested it on the project from V2. There is exactly the same bug. But I didn't notice it 😢 Can't anyone help me? 😢
Dan Harrin
Dan Harrin13mo ago
its a bug, what do you want us to do if its not already reported on github, do so
John Wink
John Wink13mo ago
Sorry I only got time to post the bug on GitHub now. I'm currently working from 8am to 8pm on a large banking project with FilamentPHP. https://github.com/filamentphp/filament/issues/9776
GitHub
Eager Loading Bug - 2...n nested Repeater · Issue #9776 · filamentp...
Package filament/filament Package Version v3.0.99 Laravel Version v10.32.1 Livewire Version No response PHP Version PHP 8.* Problem description Eager loading for 2...n nested repeaters does not wor...
John Wink
John Wink13mo ago
@AlexAnder This bug should now be solved. https://github.com/filamentphp/filament/pull/9802
GitHub
Fix nested Repeater Eager Loading by johnwinkcq · Pull Request #980...
Checks if the relationship is already eagerly loaded, returning itself to avoid querying the database again and causing an N+1 query problem. Issue/Bug Description: #9776 Changes have been thorou...
Want results from more Discord servers?
Add your server