From 9c6416fda675b62acfda5a28917660f239b203cb Mon Sep 17 00:00:00 2001 From: Tofandel Date: Tue, 15 Oct 2024 18:25:58 +0200 Subject: [PATCH] Eager load all the related items at once --- src/Repositories/Behaviors/HandleRevisions.php | 2 -- src/Repositories/BlockRepository.php | 5 ++--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Repositories/Behaviors/HandleRevisions.php b/src/Repositories/Behaviors/HandleRevisions.php index 3e489315f..bb1bef427 100644 --- a/src/Repositories/Behaviors/HandleRevisions.php +++ b/src/Repositories/Behaviors/HandleRevisions.php @@ -157,10 +157,8 @@ public function hydrateRelatedBrowsers(TwillModelContract $object, array $fields foreach ($relatedBrowsers as $browser) { $browserField = $fields['browsers'][$browser['browserName']] ?? []; - $position = 1; foreach ($browserField as $values) { - $relatedBrowserItems->push(new RelatedItem([ 'subject_id' => $object->getKey(), 'subject_type' => $object->getMorphClass(), diff --git a/src/Repositories/BlockRepository.php b/src/Repositories/BlockRepository.php index c8fff3531..86dc0ac8a 100644 --- a/src/Repositories/BlockRepository.php +++ b/src/Repositories/BlockRepository.php @@ -42,11 +42,10 @@ public function hydrate(TwillModelContract $model, array $fields): TwillModelCon 'related_type' => $item['endpointType'], 'browser_name' => $browserName, 'position' => $position, - ]) - ) - ); + ]))); $model->setRelation('relatedItems', $relatedItems); + $model->loadMissing('relatedItems.related'); return parent::hydrate($model, $fields); }