From a90a41248b4171a8538284600de7d35584ce37c3 Mon Sep 17 00:00:00 2001 From: Ernest Walzel Date: Sat, 9 Mar 2024 08:49:52 +0100 Subject: [PATCH] Use items count from ES in order to speed up page load --- app/Http/Controllers/AuthorController.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/AuthorController.php b/app/Http/Controllers/AuthorController.php index 970d11633..a903d3752 100644 --- a/app/Http/Controllers/AuthorController.php +++ b/app/Http/Controllers/AuthorController.php @@ -65,6 +65,7 @@ protected function getIndexData() $data = parent::getIndexData(); $paginator = $data['paginator']; $authorityIds = $paginator->getCollection()->pluck('id'); + $itemCounts = $paginator->getCollection()->pluck('items_count'); if ($authorityIds->isEmpty()) { return $data; @@ -74,7 +75,12 @@ protected function getIndexData() ->whereIn('id', $authorityIds) ->with(['previewItems', 'translations']) ->orderByRaw('FIELD(id, ' . $authorityIds->join(',') . ')') - ->get(); + ->get() + ->map(function (Authority $authority, $index) use ($itemCounts) { + // Use indexed items_count to speed things up + $authority->items_count = $itemCounts[$index]; + return $authority; + }); $paginator->setCollection($authorities);