Skip to content

Commit

Permalink
Flush cached table records
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Dec 6, 2023
1 parent d214638 commit ffc0610
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/tables/src/Concerns/HasBulkActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ public function getAllSelectableTableRecordsCount(): int
}

if ($this->getTable()->selectsCurrentPageOnly()) {
return $this->records->count();
return $this->cachedTableRecords->count();
}

if ($this->records instanceof LengthAwarePaginator) {
return $this->records->total();
if ($this->cachedTableRecords instanceof LengthAwarePaginator) {
return $this->cachedTableRecords->total();
}

return $this->getFilteredTableQuery()->count();
Expand Down
19 changes: 12 additions & 7 deletions packages/tables/src/Concerns/HasRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait HasRecords
*/
protected bool $allowsDuplicates = false;

protected Collection | Paginator | CursorPaginator | null $records = null;
protected Collection | Paginator | CursorPaginator | null $cachedTableRecords = null;

public function getFilteredTableQuery(): Builder
{
Expand Down Expand Up @@ -84,8 +84,8 @@ public function getTableRecords(): Collection | Paginator | CursorPaginator
$setRecordLocales = fn (Collection | Paginator | CursorPaginator $records): Collection | Paginator | CursorPaginator => $records;
}

if ($this->records) {
return $setRecordLocales($this->records);
if ($this->cachedTableRecords) {
return $setRecordLocales($this->cachedTableRecords);
}

$query = $this->getFilteredSortedTableQuery();
Expand All @@ -94,10 +94,10 @@ public function getTableRecords(): Collection | Paginator | CursorPaginator
(! $this->getTable()->isPaginated()) ||
($this->isTableReordering() && (! $this->getTable()->isPaginatedWhileReordering()))
) {
return $setRecordLocales($this->records = $this->hydratePivotRelationForTableRecords($query->get()));
return $setRecordLocales($this->cachedTableRecords = $this->hydratePivotRelationForTableRecords($query->get()));
}

return $setRecordLocales($this->records = $this->hydratePivotRelationForTableRecords($this->paginateTableQuery($query)));
return $setRecordLocales($this->cachedTableRecords = $this->hydratePivotRelationForTableRecords($this->paginateTableQuery($query)));
}

protected function resolveTableRecord(?string $key): ?Model
Expand Down Expand Up @@ -159,13 +159,18 @@ public function getTableRecordKey(Model $record): string

public function getAllTableRecordsCount(): int
{
if ($this->records instanceof LengthAwarePaginator) {
return $this->records->total();
if ($this->cachedTableRecords instanceof LengthAwarePaginator) {
return $this->cachedTableRecords->total();
}

return $this->getFilteredTableQuery()->count();
}

public function flushCachedTableRecords(): void
{
$this->cachedTableRecords = null;
}

/**
* @deprecated Override the `table()` method to configure the table.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/tables/src/Concerns/InteractsWithTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,7 @@ public function resetTable(): void
$this->resetTableFiltersForm();

$this->resetPage();

$this->flushCachedTableRecords();
}
}

0 comments on commit ffc0610

Please sign in to comment.