Skip to content

Commit

Permalink
Use items count from ES
Browse files Browse the repository at this point in the history
in order to speed up page load
  • Loading branch information
eronisko committed Mar 9, 2024
1 parent 577b785 commit a90a412
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/Http/Controllers/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit a90a412

Please sign in to comment.