Skip to content

Commit bce28db

Browse files
committed
Query Rework
- Add property for the builder instance - Refactor to get and set that single instance - Remove all mounted or booted traits and things are being executed in the wrong order - Call them all in format before the view renders - Boot the columns to empty to prevent uninitialized error - Initially call query in boot then call before every render (needed for bulk actions) - Refactor bulk actions to clone the current query
1 parent a93fa1d commit bce28db

16 files changed

+90
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ phpunit.xml
99
psalm.xml
1010
testbench.yaml
1111
vendor
12+
output.txt

src/DataTableComponent.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ public function boot(): void
5959

6060
// Set the filter defaults based on the filter type
6161
$this->setFilterDefaults();
62-
63-
// Set the user defined columns to work with
64-
$this->setColumns();
6562

6663
// Call the child configuration, if any
6764
$this->configure();
@@ -78,6 +75,8 @@ public function boot(): void
7875
public function booted(): void
7976
{
8077
$this->setTheme();
78+
$this->setBuilder($this->builder());
79+
$this->setColumns();
8180
}
8281

8382
/**
@@ -119,6 +118,14 @@ public function customView(): string
119118
*/
120119
public function render()
121120
{
121+
$this->setBuilder($this->builder());
122+
$this->setColumns();
123+
$this->setupColumnSelect();
124+
$this->setupPagination();
125+
$this->setupSecondaryHeader();
126+
$this->setupFooter();
127+
$this->setupReordering();
128+
122129
return view('livewire-tables::datatable')
123130
->with([
124131
'columns' => $this->getColumns(),

src/Traits/ComponentUtilities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Support\Str;
67
use Rappasoft\LaravelLivewireTables\Traits\Configuration\ComponentConfiguration;
78
use Rappasoft\LaravelLivewireTables\Traits\Helpers\ComponentHelpers;
@@ -13,6 +14,7 @@ trait ComponentUtilities
1314

1415
public array $table = [];
1516
public $theme = null;
17+
protected Builder $builder;
1618
protected $model;
1719
protected $primaryKey;
1820
protected string $tableName = 'table';

src/Traits/Helpers/BulkActionsHelpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function updatedSelected(): void
172172
*/
173173
public function updatedSelectAll(): void
174174
{
175-
if (count($this->getSelected()) === $this->baseQuery()->pluck($this->getPrimaryKey())->count()) {
175+
if (count($this->getSelected()) === (clone $this->baseQuery())->pluck($this->getPrimaryKey())->count()) {
176176
$this->clearSelected();
177177
} else {
178178
$this->setAllSelected();
@@ -185,6 +185,6 @@ public function updatedSelectAll(): void
185185
public function setAllSelected(): void
186186
{
187187
$this->setSelectAllEnabled();
188-
$this->setSelected($this->baseQuery()->pluck($this->getPrimaryKey())->map(fn ($item) => (string)$item)->toArray());
188+
$this->setSelected((clone $this->baseQuery())->pluck($this->getPrimaryKey())->map(fn ($item) => (string)$item)->toArray());
189189
}
190190
}

src/Traits/Helpers/ColumnHelpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setColumns(): void
1919

2020
if ($column->hasField()) {
2121
if ($column->isBaseColumn()) {
22-
$column->setTable($this->builder()->getModel()->getTable());
22+
$column->setTable($this->getBuilder()->getModel()->getTable());
2323
} else {
2424
$column->setTable($this->getTableForColumn($column));
2525
}

src/Traits/Helpers/ComponentHelpers.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits\Helpers;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Model;
67
use Rappasoft\LaravelLivewireTables\Views\Column;
78

89
trait ComponentHelpers
910
{
11+
/**
12+
* @param Builder
13+
*/
14+
public function setBuilder(Builder $builder): void
15+
{
16+
$this->builder = $builder;
17+
}
18+
19+
/**
20+
* @return Builder
21+
*/
22+
public function getBuilder(): Builder
23+
{
24+
return $this->builder;
25+
}
26+
1027
/**
1128
* @return bool
1229
*/

src/Traits/WithColumnSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trait WithColumnSelect
1414
protected bool $columnSelectStatus = true;
1515
protected bool $rememberColumnSelectionStatus = true;
1616

17-
public function mountWithColumnSelect(): void
17+
public function setupColumnSelect(): void
1818
{
1919
// If remember selection is off, then clear the session
2020
if ($this->rememberColumnSelectionIsDisabled()) {

src/Traits/WithColumns.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ trait WithColumns
1010
use ColumnHelpers;
1111

1212
protected Collection $columns;
13+
14+
public function bootWithColumns(): void
15+
{
16+
$this->columns = collect();
17+
}
1318
}

src/Traits/WithData.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,58 @@ trait WithData
1212
// TODO: Test
1313
public function getRows()
1414
{
15-
return $this->executeQuery($this->baseQuery());
15+
$this->baseQuery();
16+
17+
return $this->executeQuery();
1618
}
1719

1820
protected function baseQuery(): Builder
1921
{
20-
$builder = $this->joinRelations($this->builder());
22+
$this->setBuilder($this->joinRelations());
2123

22-
$builder = $this->selectFields($builder);
24+
$this->setBuilder($this->selectFields());
2325

24-
$builder = $this->applySearch($builder);
26+
$this->setBuilder($this->applySearch());
2527

26-
$builder = $this->applyFilters($builder);
28+
$this->setBuilder($this->applyFilters());
2729

2830
if ($this->currentlyReorderingIsEnabled()) {
29-
return $builder->orderBy($this->getDefaultReorderColumn(), $this->getDefaultReorderDirection());
31+
$this->setBuilder($this->getBuilder()->orderBy($this->getDefaultReorderColumn(), $this->getDefaultReorderDirection()));
32+
33+
return $this->getBuilder();
3034
}
3135

32-
return $this->applySorting($builder);
36+
return $this->applySorting();
3337
}
3438

35-
protected function executeQuery(Builder $builder)
39+
protected function executeQuery()
3640
{
3741
return $this->paginationIsEnabled() ?
38-
$builder->paginate($this->getPerPage() === -1 ? $builder->count() : $this->getPerPage(), ['*'], $this->getComputedPageName()) :
39-
$builder->get();
42+
$this->getBuilder()->paginate($this->getPerPage() === -1 ? $this->getBuilder()->count() : $this->getPerPage(), ['*'], $this->getComputedPageName()) :
43+
$this->getBuilder()->get();
4044
}
4145

42-
protected function joinRelations(Builder $builder): Builder
46+
protected function joinRelations(): Builder
4347
{
4448
foreach ($this->getSelectableColumns() as $column) {
4549
if ($column->hasRelations()) {
46-
$builder = $this->joinRelation($builder, $column);
50+
$this->setBuilder($this->joinRelation($column));
4751
}
4852
}
4953

50-
return $builder;
54+
return $this->getBuilder();
5155
}
5256

53-
protected function joinRelation(Builder $builder, Column $column): Builder
57+
protected function joinRelation(Column $column): Builder
5458
{
5559
if ($column->eagerLoadRelationsIsEnabled() || $this->eagerLoadAllRelationsIsEnabled()) {
56-
$builder->with($column->getRelationString());
60+
$this->setBuilder($this->getBuilder()->with($column->getRelationString()));
5761
}
5862

5963
$table = false;
6064
$foreign = false;
6165
$other = false;
62-
$lastQuery = $builder;
66+
$lastQuery = clone $this->getBuilder();
6367

6468
foreach ($column->getRelations() as $relationPart) {
6569
$model = $lastQuery->getRelation($relationPart);
@@ -81,48 +85,48 @@ protected function joinRelation(Builder $builder, Column $column): Builder
8185
}
8286

8387
if ($table) {
84-
$builder = $this->performJoin($builder, $table, $foreign, $other);
88+
$this->setBuilder($this->performJoin($table, $foreign, $other));
8589
}
8690

8791
$lastQuery = $model->getQuery();
8892
}
8993

90-
return $builder;
94+
return $this->getBuilder();
9195
}
9296

93-
protected function performJoin(Builder $builder, $table, $foreign, $other, $type = 'left'): Builder
97+
protected function performJoin($table, $foreign, $other, $type = 'left'): Builder
9498
{
9599
$joins = [];
96100

97-
foreach ($builder->getQuery()->joins ?? [] as $join) {
101+
foreach ($this->getBuilder()->getQuery()->joins ?? [] as $join) {
98102
$joins[] = $join->table;
99103
}
100104

101105
if (! in_array($table, $joins, true)) {
102-
$builder->join($table, $foreign, '=', $other, $type);
106+
$this->setBuilder($this->getBuilder()->join($table, $foreign, '=', $other, $type));
103107
}
104108

105-
return $builder;
109+
return $this->getBuilder();
106110
}
107111

108-
protected function selectFields(Builder $builder): Builder
112+
protected function selectFields(): Builder
109113
{
110114
// Load any additional selects that were not already columns
111115
foreach ($this->getAdditionalSelects() as $select) {
112-
$builder->addSelect($select);
116+
$this->setBuilder($this->getBuilder()->addSelect($select));
113117
}
114118

115119
foreach ($this->getSelectableColumns() as $column) {
116-
$builder->addSelect($column->getColumn() . ' as ' .$column->getColumnSelectName());
120+
$this->setBuilder($this->getBuilder()->addSelect($column->getColumn() . ' as ' .$column->getColumnSelectName()));
117121
}
118122

119-
return $builder;
123+
return $this->getBuilder();
120124
}
121125

122126
protected function getTableForColumn(Column $column): ?string
123127
{
124128
$table = null;
125-
$lastQuery = $this->builder();
129+
$lastQuery = clone $this->getBuilder();
126130

127131
foreach ($column->getRelations() as $relationPart) {
128132
$model = $lastQuery->getRelation($relationPart);

src/Traits/WithFilters.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function filters(): array
2121
return [];
2222
}
2323

24-
public function applyFilters(Builder $builder): Builder
24+
public function applyFilters(): Builder
2525
{
2626
if ($this->filtersAreEnabled() && $this->hasFilters() && $this->hasAppliedFiltersWithValues()) {
2727
foreach ($this->getFilters() as $filter) {
@@ -34,12 +34,12 @@ public function applyFilters(Builder $builder): Builder
3434
continue;
3535
}
3636

37-
($filter->getFilterCallback())($builder, $value);
37+
($filter->getFilterCallback())($this->getBuilder(), $value);
3838
}
3939
}
4040
}
4141
}
4242

43-
return $builder;
43+
return $this->getBuilder();
4444
}
4545
}

0 commit comments

Comments
 (0)