From 38f421c5d9e3757af8c7a726a40b4469b6ed4046 Mon Sep 17 00:00:00 2001 From: Luan Freitas <33601626+luanfreitasdev@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:29:36 -0300 Subject: [PATCH] fix entangle detail with cache (#1180) --- src/PowerGridComponent.php | 27 +++++++++++++++++++++++++++ src/ProcessDataSource.php | 30 +----------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/PowerGridComponent.php b/src/PowerGridComponent.php index f1900d25..f0cc488a 100644 --- a/src/PowerGridComponent.php +++ b/src/PowerGridComponent.php @@ -340,6 +340,31 @@ private function renderView(mixed $data): Application|Factory|View ]); } + private function resolveDetailRow(mixed $results): void + { + if (!isset($this->setUp['detail'])) { + return; + } + + $collection = $results; + + if (!$results instanceof BaseCollection) { + /** @phpstan-ignore-next-line */ + $collection = collect($results->items()); + } + + /** @phpstan-ignore-next-line */ + $collection->each(function ($model) { + $id = strval($model->{$this->primaryKey}); + + data_set($this->setUp, 'detail', (array) $this->setUp['detail']); + + $state = data_get($this->setUp, 'detail.state.' . $id, false); + + data_set($this->setUp, 'detail.state.' . $id, $state); + }); + } + /** * @throws Exception|Throwable */ @@ -361,6 +386,8 @@ public function render(): Application|Factory|View $data = $this->getCachedData(); + $this->resolveDetailRow($data); + /** @phpstan-ignore-next-line */ $this->totalCurrentPage = method_exists($data, 'items') ? count($data->items()) : $data->count(); diff --git a/src/ProcessDataSource.php b/src/ProcessDataSource.php index 52ee71b4..c2b99a4a 100644 --- a/src/ProcessDataSource.php +++ b/src/ProcessDataSource.php @@ -87,9 +87,7 @@ private function processCollection(mixed $datasource): \Illuminate\Pagination\Le $paginated = Collection::paginate($results, intval(data_get($this->component->setUp, 'footer.perPage'))); $results = $paginated->setCollection($this->transform($paginated->getCollection())); - } - - self::resolveDetailRow($results); + }; return $results; } @@ -124,8 +122,6 @@ private function processModel(EloquentBuilder|MorphToMany|QueryBuilder|BaseColle $results = $this->applyPerPage($results); - $this->resolveDetailRow($results); - $this->setTotalCount($results); /** @phpstan-ignore-next-line */ @@ -225,30 +221,6 @@ private function applyPerPage(EloquentBuilder|QueryBuilder|MorphToMany $results) return $results->$paginate($results->count()); } - private function resolveDetailRow(Paginator|LengthAwarePaginator|BaseCollection $results): void - { - if (!isset($this->component->setUp['detail'])) { - return; - } - - $collection = $results; - - if (!$results instanceof BaseCollection) { - $collection = collect($results->items()); - } - - /** @phpstan-ignore-next-line */ - $collection->each(function ($model) { - $id = strval($model->{$this->component->primaryKey}); - - data_set($this->component->setUp, 'detail', (array) $this->component->setUp['detail']); - - $state = data_get($this->component->setUp, 'detail.state.' . $id, false); - - data_set($this->component->setUp, 'detail.state.' . $id, $state); - }); - } - /** * @throws \Exception */