From ff9dfd14761cb8c3d8a266dd8aad3f29caa2dde8 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:58:49 +0100 Subject: [PATCH 01/66] Initial Commit --- src/Traits/ComponentUtilities.php | 4 ++ .../Configuration/ComponentConfiguration.php | 45 +++++++++++++++++++ src/Traits/Helpers/ComponentHelpers.php | 21 +++++++++ src/Traits/WithData.php | 4 +- 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 1e421e0b6..1c7e9d2b9 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -37,6 +37,10 @@ trait ComponentUtilities protected array $additionalSelects = []; + protected array $extraWiths = []; + + protected array $extraWithCounts = []; + /** * Set any configuration options */ diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 90611185a..e0d08d1f4 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -96,4 +96,49 @@ public function setDataTableFingerprint(string $dataTableFingerprint): self return $this; } + + public function setExtraWiths(array $extraWiths): self + { + $this->extraWiths = $extraWiths; + + return $this; + } + + public function addExtraWith(string $extraWith): self + { + $this->extraWiths[] = $extraWith; + + return $this; + } + + public function addExtraWiths(array $extraWiths): self + { + $this->extraWiths[] = [...$this->extraWiths, ...$extraWiths]; + + return $this; + } + + + public function setExtraWithCounts(array $extraWithCounts): self + { + $this->extraWithCounts = $extraWithCounts; + + return $this; + } + + public function addExtraWithCount(string $extraWithCount): self + { + $this->extraWithCounts[] = $extraWithCount; + + return $this; + } + + public function addExtraWithCounts(array $extraWithCounts): self + { + $this->extraWithCounts[] = [...$this->extraWithCounts, ...$extraWithCounts]; + + return $this; + } + + } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index c600200b5..080ebcd25 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -149,4 +149,25 @@ public function getAdditionalSelects(): array { return $this->additionalSelects; } + + public function hasExtraWiths(): bool + { + return !empty($this->extraWiths); + } + + public function getExtraWiths(): array + { + return $this->extraWiths; + } + + + public function hasExtraWithCounts(): bool + { + return !empty($this->extraWithCounts); + } + + public function getExtraWithCounts(): array + { + return $this->extraWithCounts; + } } diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 5e1ca3298..daf0ed5e1 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -249,7 +249,9 @@ protected function getTableAlias(?string $currentTableAlias, string $relationPar public function builder(): Builder { if ($this->hasModel()) { - return $this->getModel()::query()->with($this->getRelationships()); + return $this->getModel()::query() + ->with([...$this->getExtraWiths(), ...$this->getRelationships()]) + ->when($this->hasExtraWithCounts(), fn ($query) => $query->withCount($this->extraWithCounts)); } // If model does not exist From be1b208088167412e6f6fb41b3de4e7609c13fed Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 19:59:12 +0000 Subject: [PATCH 02/66] Fix styling --- src/Traits/Configuration/ComponentConfiguration.php | 3 --- src/Traits/Helpers/ComponentHelpers.php | 7 +++---- src/Traits/WithData.php | 4 ++-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index e0d08d1f4..03a24a73d 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -118,7 +118,6 @@ public function addExtraWiths(array $extraWiths): self return $this; } - public function setExtraWithCounts(array $extraWithCounts): self { $this->extraWithCounts = $extraWithCounts; @@ -139,6 +138,4 @@ public function addExtraWithCounts(array $extraWithCounts): self return $this; } - - } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 080ebcd25..0c8756fd9 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -152,7 +152,7 @@ public function getAdditionalSelects(): array public function hasExtraWiths(): bool { - return !empty($this->extraWiths); + return ! empty($this->extraWiths); } public function getExtraWiths(): array @@ -160,12 +160,11 @@ public function getExtraWiths(): array return $this->extraWiths; } - public function hasExtraWithCounts(): bool { - return !empty($this->extraWithCounts); + return ! empty($this->extraWithCounts); } - + public function getExtraWithCounts(): array { return $this->extraWithCounts; diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index daf0ed5e1..69c34705a 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -250,8 +250,8 @@ public function builder(): Builder { if ($this->hasModel()) { return $this->getModel()::query() - ->with([...$this->getExtraWiths(), ...$this->getRelationships()]) - ->when($this->hasExtraWithCounts(), fn ($query) => $query->withCount($this->extraWithCounts)); + ->with([...$this->getExtraWiths(), ...$this->getRelationships()]) + ->when($this->hasExtraWithCounts(), fn ($query) => $query->withCount($this->extraWithCounts)); } // If model does not exist From f1faef6ba3d4d90a1d365f2d4e37841354b53192 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:41:15 +0100 Subject: [PATCH 03/66] Adjust CountColumn --- src/Traits/WithData.php | 9 ++++-- src/Views/Columns/CountColumn.php | 30 +++++++++++++++++++ .../CountColumnConfiguration.php | 21 +++++++++++++ .../Traits/Helpers/CountColumnHelpers.php | 21 +++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/Views/Columns/CountColumn.php create mode 100644 src/Views/Traits/Configuration/CountColumnConfiguration.php create mode 100644 src/Views/Traits/Helpers/CountColumnHelpers.php diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 69c34705a..40bf3c879 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -56,6 +56,12 @@ protected function baseQuery(): Builder $this->setBuilder($this->applyFilters()); + if ($this->hasExtraWithCounts()) + { + $this->setBuilder($this->getBuilder()->withCount($this->getExtraWithCounts())); + } + + return $this->getBuilder(); } @@ -250,8 +256,7 @@ public function builder(): Builder { if ($this->hasModel()) { return $this->getModel()::query() - ->with([...$this->getExtraWiths(), ...$this->getRelationships()]) - ->when($this->hasExtraWithCounts(), fn ($query) => $query->withCount($this->extraWithCounts)); + ->with($this->getRelationships()) } // If model does not exist diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php new file mode 100644 index 000000000..e8e774780 --- /dev/null +++ b/src/Views/Columns/CountColumn.php @@ -0,0 +1,30 @@ +label(fn () => null); + } + } + + + + +} diff --git a/src/Views/Traits/Configuration/CountColumnConfiguration.php b/src/Views/Traits/Configuration/CountColumnConfiguration.php new file mode 100644 index 000000000..2d763d2c8 --- /dev/null +++ b/src/Views/Traits/Configuration/CountColumnConfiguration.php @@ -0,0 +1,21 @@ +countSource = $countSource; + $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'} ); + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn(Builder $query, string $direction) => $query->orderBy($this->countSource."_count", $direction) : $callback; + + return $this; + } +} diff --git a/src/Views/Traits/Helpers/CountColumnHelpers.php b/src/Views/Traits/Helpers/CountColumnHelpers.php new file mode 100644 index 000000000..fa3632049 --- /dev/null +++ b/src/Views/Traits/Helpers/CountColumnHelpers.php @@ -0,0 +1,21 @@ +countSource; + } + + public function hasCountSource():bool + { + return isset($this->countSource); + } + +} From 2e3fdf83bb0f5f4d56b23c8bb1d35d47c5e1bec8 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:42:09 +0100 Subject: [PATCH 04/66] Fix buildedr --- src/Traits/WithData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 40bf3c879..2aa1e481a 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -256,7 +256,7 @@ public function builder(): Builder { if ($this->hasModel()) { return $this->getModel()::query() - ->with($this->getRelationships()) + ->with($this->getRelationships()); } // If model does not exist From 474d7d15e31bbfd5acbee6205d233400653b5b63 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 20:42:31 +0000 Subject: [PATCH 05/66] Fix styling --- src/Traits/WithData.php | 4 +- src/Views/Columns/CountColumn.php | 56 +++++++++---------- .../CountColumnConfiguration.php | 43 +++++++------- .../Traits/Helpers/CountColumnHelpers.php | 41 +++++++------- 4 files changed, 69 insertions(+), 75 deletions(-) diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 2aa1e481a..01672cd52 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -56,11 +56,9 @@ protected function baseQuery(): Builder $this->setBuilder($this->applyFilters()); - if ($this->hasExtraWithCounts()) - { + if ($this->hasExtraWithCounts()) { $this->setBuilder($this->getBuilder()->withCount($this->getExtraWithCounts())); } - return $this->getBuilder(); diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index e8e774780..dedd7698c 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -1,30 +1,26 @@ -label(fn () => null); - } - } - - - - -} +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/CountColumnConfiguration.php b/src/Views/Traits/Configuration/CountColumnConfiguration.php index 2d763d2c8..cd79d2f38 100644 --- a/src/Views/Traits/Configuration/CountColumnConfiguration.php +++ b/src/Views/Traits/Configuration/CountColumnConfiguration.php @@ -1,21 +1,22 @@ -countSource = $countSource; - $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'} ); - return $this; - } - - public function sortable(?callable $callback = null): self - { - $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn(Builder $query, string $direction) => $query->orderBy($this->countSource."_count", $direction) : $callback; - - return $this; - } -} +countSource = $countSource; + $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'}); + + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->countSource.'_count', $direction) : $callback; + + return $this; + } +} diff --git a/src/Views/Traits/Helpers/CountColumnHelpers.php b/src/Views/Traits/Helpers/CountColumnHelpers.php index fa3632049..7f10d44e3 100644 --- a/src/Views/Traits/Helpers/CountColumnHelpers.php +++ b/src/Views/Traits/Helpers/CountColumnHelpers.php @@ -1,21 +1,20 @@ -countSource; - } - - public function hasCountSource():bool - { - return isset($this->countSource); - } - -} +countSource; + } + + public function hasCountSource(): bool + { + return isset($this->countSource); + } +} From fc20d5f3e3a5f73bdcf8ed6c16d44e3f9a83b7dd Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:49:09 +0100 Subject: [PATCH 06/66] Fixes --- src/Traits/Helpers/ColumnHelpers.php | 22 ++++++++ src/Views/Columns/CountColumn.php | 52 +++++++++---------- .../CountColumnConfiguration.php | 47 +++++++++-------- 3 files changed, 73 insertions(+), 48 deletions(-) diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index 009a6f4bd..4c59762a6 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Rappasoft\LaravelLivewireTables\Views\Column; +use Rappasoft\LaravelLivewireTables\Views\Columns\CountColumn; trait ColumnHelpers { @@ -18,6 +19,13 @@ public function setColumns(): void ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); + if ($column instanceof CountColumn) + { + if ($column->hasCountSource()) + { + $this->addExtraWithCount($column->getCountSource()); + } + } if ($column->hasField()) { if ($column->isBaseColumn()) { @@ -198,6 +206,13 @@ public function getPrependedColumns(): Collection ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); + if ($column instanceof CountColumn) + { + if ($column->hasCountSource()) + { + $this->addExtraWithCount($column->getCountSource()); + } + } if ($column->hasField()) { if ($column->isBaseColumn()) { @@ -217,6 +232,13 @@ public function getAppendedColumns(): Collection ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); + if ($column instanceof CountColumn) + { + if ($column->hasCountSource()) + { + $this->addExtraWithCount($column->getCountSource()); + } + } if ($column->hasField()) { if ($column->isBaseColumn()) { diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index dedd7698c..90f51efd6 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -1,26 +1,26 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/CountColumnConfiguration.php b/src/Views/Traits/Configuration/CountColumnConfiguration.php index cd79d2f38..999209448 100644 --- a/src/Views/Traits/Configuration/CountColumnConfiguration.php +++ b/src/Views/Traits/Configuration/CountColumnConfiguration.php @@ -1,22 +1,25 @@ -countSource = $countSource; - $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'}); - - return $this; - } - - public function sortable(?callable $callback = null): self - { - $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->countSource.'_count', $direction) : $callback; - - return $this; - } -} +countSource = $countSource; + $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'}); + + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->countSource.'_count', $direction) : $callback; + + return $this; + } +} From d8a03a37cac4042fc1502149197199ed95efb534 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 20:49:32 +0000 Subject: [PATCH 07/66] Fix styling --- src/Traits/Helpers/ColumnHelpers.php | 18 +++---- src/Views/Columns/CountColumn.php | 52 +++++++++---------- .../CountColumnConfiguration.php | 50 +++++++++--------- 3 files changed, 57 insertions(+), 63 deletions(-) diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index 4c59762a6..a697c3b61 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -19,10 +19,8 @@ public function setColumns(): void ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); - if ($column instanceof CountColumn) - { - if ($column->hasCountSource()) - { + if ($column instanceof CountColumn) { + if ($column->hasCountSource()) { $this->addExtraWithCount($column->getCountSource()); } } @@ -206,10 +204,8 @@ public function getPrependedColumns(): Collection ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); - if ($column instanceof CountColumn) - { - if ($column->hasCountSource()) - { + if ($column instanceof CountColumn) { + if ($column->hasCountSource()) { $this->addExtraWithCount($column->getCountSource()); } } @@ -232,10 +228,8 @@ public function getAppendedColumns(): Collection ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); - if ($column instanceof CountColumn) - { - if ($column->hasCountSource()) - { + if ($column instanceof CountColumn) { + if ($column->hasCountSource()) { $this->addExtraWithCount($column->getCountSource()); } } diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 90f51efd6..063b9ed9b 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -1,26 +1,26 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/CountColumnConfiguration.php b/src/Views/Traits/Configuration/CountColumnConfiguration.php index 999209448..3aabcd284 100644 --- a/src/Views/Traits/Configuration/CountColumnConfiguration.php +++ b/src/Views/Traits/Configuration/CountColumnConfiguration.php @@ -1,25 +1,25 @@ -countSource = $countSource; - $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'}); - - return $this; - } - - public function sortable(?callable $callback = null): self - { - $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->countSource.'_count', $direction) : $callback; - - return $this; - } -} +countSource = $countSource; + $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'}); + + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->countSource.'_count', $direction) : $callback; + + return $this; + } +} From 162182990a97e2f638c67d2ab8d6e467c484ce9c Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:07:53 +0100 Subject: [PATCH 08/66] Add ExtraWiths --- src/Traits/WithData.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 01672cd52..eb8e2bb5d 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -55,6 +55,17 @@ protected function baseQuery(): Builder $this->setBuilder($this->applySearch()); $this->setBuilder($this->applyFilters()); + + if ($this->hasExtraWiths()) { + //. $builder = $this->getBuilder(); + // foreach($this->getExtraWiths() as $extraWith) + // { + // $builder->with($extraWith); + // } + // $this->setBuilder($builder); + $this->setBuilder($this->getBuilder()->with($this->getExtraWiths())); + + } if ($this->hasExtraWithCounts()) { $this->setBuilder($this->getBuilder()->withCount($this->getExtraWithCounts())); From baf668e0186378ad9de8da6ff5be859de1b1a604 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 21:08:16 +0000 Subject: [PATCH 09/66] Fix styling --- src/Traits/WithData.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index eb8e2bb5d..d3dd1f39a 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -55,14 +55,14 @@ protected function baseQuery(): Builder $this->setBuilder($this->applySearch()); $this->setBuilder($this->applyFilters()); - + if ($this->hasExtraWiths()) { - //. $builder = $this->getBuilder(); - // foreach($this->getExtraWiths() as $extraWith) - // { - // $builder->with($extraWith); - // } - // $this->setBuilder($builder); + //. $builder = $this->getBuilder(); + // foreach($this->getExtraWiths() as $extraWith) + // { + // $builder->with($extraWith); + // } + // $this->setBuilder($builder); $this->setBuilder($this->getBuilder()->with($this->getExtraWiths())); } From 5ebf0221fb59cb76b641be01086d3e5a7d4f4b0f Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:21:09 +0100 Subject: [PATCH 10/66] Add AggregateColumn --- src/Views/Columns/AggregateColumn.php | 27 ++++++++++++++++ src/Views/Columns/CountColumn.php | 1 - .../AggregateColumnConfiguration.php | 32 +++++++++++++++++++ .../Traits/Helpers/AggregateColumnHelpers.php | 26 +++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/Views/Columns/AggregateColumn.php create mode 100644 src/Views/Traits/Configuration/AggregateColumnConfiguration.php create mode 100644 src/Views/Traits/Helpers/AggregateColumnHelpers.php diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php new file mode 100644 index 000000000..27f7cbf1c --- /dev/null +++ b/src/Views/Columns/AggregateColumn.php @@ -0,0 +1,27 @@ +label(fn () => null); + } + } +} diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 063b9ed9b..c119b069c 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -2,7 +2,6 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Illuminate\Database\Eloquent\{Builder, Model}; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\CountColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\CountColumnHelpers; diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php new file mode 100644 index 000000000..8268b9c35 --- /dev/null +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -0,0 +1,32 @@ +dataSource = $dataSource; + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()}); + + return $this; + } + + public function setAggregateMethod(string $aggregateMethod): self + { + $this->aggregateMethod = $aggregateMethod; + + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod(), $direction) : $callback; + + return $this; + } +} diff --git a/src/Views/Traits/Helpers/AggregateColumnHelpers.php b/src/Views/Traits/Helpers/AggregateColumnHelpers.php new file mode 100644 index 000000000..a101df544 --- /dev/null +++ b/src/Views/Traits/Helpers/AggregateColumnHelpers.php @@ -0,0 +1,26 @@ +dataSource; + } + + public function hasDataSource(): bool + { + return isset($this->dataSource); + } + + public function getAggregateMethod(): string + { + return $this->aggregateMethod; + } + +} From 6b390259ad2f41cc1077a51b2b9ff83ce49d3b92 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 21:21:45 +0000 Subject: [PATCH 11/66] Fix styling --- src/Views/Columns/AggregateColumn.php | 54 ++++++++-------- .../AggregateColumnConfiguration.php | 64 +++++++++---------- .../Traits/Helpers/AggregateColumnHelpers.php | 51 ++++++++------- 3 files changed, 84 insertions(+), 85 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 27f7cbf1c..7007bc4ec 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -1,27 +1,27 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php index 8268b9c35..95a1f4161 100644 --- a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -1,32 +1,32 @@ -dataSource = $dataSource; - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()}); - - return $this; - } - - public function setAggregateMethod(string $aggregateMethod): self - { - $this->aggregateMethod = $aggregateMethod; - - return $this; - } - - public function sortable(?callable $callback = null): self - { - $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod(), $direction) : $callback; - - return $this; - } -} +dataSource = $dataSource; + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()}); + + return $this; + } + + public function setAggregateMethod(string $aggregateMethod): self + { + $this->aggregateMethod = $aggregateMethod; + + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod(), $direction) : $callback; + + return $this; + } +} diff --git a/src/Views/Traits/Helpers/AggregateColumnHelpers.php b/src/Views/Traits/Helpers/AggregateColumnHelpers.php index a101df544..2bd77e983 100644 --- a/src/Views/Traits/Helpers/AggregateColumnHelpers.php +++ b/src/Views/Traits/Helpers/AggregateColumnHelpers.php @@ -1,26 +1,25 @@ -dataSource; - } - - public function hasDataSource(): bool - { - return isset($this->dataSource); - } - - public function getAggregateMethod(): string - { - return $this->aggregateMethod; - } - -} +dataSource; + } + + public function hasDataSource(): bool + { + return isset($this->dataSource); + } + + public function getAggregateMethod(): string + { + return $this->aggregateMethod; + } +} From 9d3b2bda20f4d5b8f37f04b9460cdeae899a15c6 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:26:20 +0100 Subject: [PATCH 12/66] Add SumColumn --- src/Views/Columns/SumColumn.php | 27 ++++++++++++++++ .../Configuration/SumColumnConfiguration.php | 32 +++++++++++++++++++ src/Views/Traits/Helpers/SumColumnHelpers.php | 25 +++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/Views/Columns/SumColumn.php create mode 100644 src/Views/Traits/Configuration/SumColumnConfiguration.php create mode 100644 src/Views/Traits/Helpers/SumColumnHelpers.php diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php new file mode 100644 index 000000000..d0d2f3c60 --- /dev/null +++ b/src/Views/Columns/SumColumn.php @@ -0,0 +1,27 @@ +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/SumColumnConfiguration.php b/src/Views/Traits/Configuration/SumColumnConfiguration.php new file mode 100644 index 000000000..24ddd223f --- /dev/null +++ b/src/Views/Traits/Configuration/SumColumnConfiguration.php @@ -0,0 +1,32 @@ +dataSource = $dataSource; + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()}); + + return $this; + } + + public function setAggregateMethod(string $aggregateMethod): self + { + $this->aggregateMethod = $aggregateMethod; + + return $this; + } + + public function sortable(?callable $callback = null): self + { + $this->sortable = true; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod(), $direction) : $callback; + + return $this; + } +} diff --git a/src/Views/Traits/Helpers/SumColumnHelpers.php b/src/Views/Traits/Helpers/SumColumnHelpers.php new file mode 100644 index 000000000..9c97576dd --- /dev/null +++ b/src/Views/Traits/Helpers/SumColumnHelpers.php @@ -0,0 +1,25 @@ +dataSource; + } + + public function hasDataSource(): bool + { + return isset($this->dataSource); + } + + public function getAggregateMethod(): string + { + return $this->aggregateMethod; + } +} From 87f7b2fad49e7d603537dbef779ab5750f11b40f Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 21:26:42 +0000 Subject: [PATCH 13/66] Fix styling --- src/Views/Columns/SumColumn.php | 54 ++++++++++++++++----------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index d0d2f3c60..0175a5f1a 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -1,27 +1,27 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} From 02b4f6f935c1f24151e14482f363d40e1a6a0edb Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:44:43 +0100 Subject: [PATCH 14/66] Standardise to setDataSource --- src/Traits/ComponentUtilities.php | 2 + .../Configuration/ComponentConfiguration.php | 8 +++ src/Traits/Helpers/ColumnHelpers.php | 32 ++++++++--- src/Traits/Helpers/ComponentHelpers.php | 12 ++++ src/Traits/WithData.php | 21 ++++--- src/Views/Columns/CountColumn.php | 2 +- src/Views/Columns/SumColumn.php | 56 ++++++++++--------- .../CountColumnConfiguration.php | 8 +-- .../Configuration/SumColumnConfiguration.php | 9 ++- .../Traits/Helpers/CountColumnHelpers.php | 8 +-- src/Views/Traits/Helpers/SumColumnHelpers.php | 6 ++ 11 files changed, 110 insertions(+), 54 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 1c7e9d2b9..9a5e5e985 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -40,6 +40,8 @@ trait ComponentUtilities protected array $extraWiths = []; protected array $extraWithCounts = []; + + protected array $extraWithSums = []; /** * Set any configuration options diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 03a24a73d..ac02a72b7 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -138,4 +138,12 @@ public function addExtraWithCounts(array $extraWithCounts): self return $this; } + + public function addExtraWithSum(string $relationship, string $column): self + { + $this->extraWithSums[$relationship] = $column; + + return $this; + } + } diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index a697c3b61..61ef60fa2 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -4,7 +4,7 @@ use Illuminate\Support\Collection; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Columns\CountColumn; +use Rappasoft\LaravelLivewireTables\Views\Columns\{CountColumn,SumColumn}; trait ColumnHelpers { @@ -20,8 +20,14 @@ public function setColumns(): void ->map(function (Column $column) { $column->setComponent($this); if ($column instanceof CountColumn) { - if ($column->hasCountSource()) { - $this->addExtraWithCount($column->getCountSource()); + if ($column->hasDataSource()) { + $this->addExtraWithCount($column->getDataSource()); + } + } + + if ($column instanceof SumColumn) { + if ($column->hasDataSource()) { + $this->addExtraWithSum($column->getDataSource(), $column->getSumColumn()); } } @@ -205,8 +211,14 @@ public function getPrependedColumns(): Collection ->map(function (Column $column) { $column->setComponent($this); if ($column instanceof CountColumn) { - if ($column->hasCountSource()) { - $this->addExtraWithCount($column->getCountSource()); + if ($column->hasDataSource()) { + $this->addExtraWithCount($column->getDataSource()); + } + } + + if ($column instanceof SumColumn) { + if ($column->hasDataSource()) { + $this->addExtraWithSum($column->getDataSource(), $column->getSumColumn()); } } @@ -229,8 +241,14 @@ public function getAppendedColumns(): Collection ->map(function (Column $column) { $column->setComponent($this); if ($column instanceof CountColumn) { - if ($column->hasCountSource()) { - $this->addExtraWithCount($column->getCountSource()); + if ($column->hasDataSource()) { + $this->addExtraWithCount($column->getDataSource()); + } + } + + if ($column instanceof SumColumn) { + if ($column->hasDataSource()) { + $this->addExtraWithSum($column->getDataSource(), $column->getSumColumn()); } } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 0c8756fd9..422c978d8 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -169,4 +169,16 @@ public function getExtraWithCounts(): array { return $this->extraWithCounts; } + + public function hasExtraWithSums(): bool + { + return ! empty($this->extraWithSums); + } + + public function getExtraWithSums(): array + { + return $this->extraWithSums; + } + + } diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index d3dd1f39a..92685c0c2 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -56,20 +56,25 @@ protected function baseQuery(): Builder $this->setBuilder($this->applyFilters()); + $builder = $this->getBuilder(); + if ($this->hasExtraWiths()) { - //. $builder = $this->getBuilder(); - // foreach($this->getExtraWiths() as $extraWith) - // { - // $builder->with($extraWith); - // } - // $this->setBuilder($builder); - $this->setBuilder($this->getBuilder()->with($this->getExtraWiths())); + $builder->with($this->getExtraWiths()); + } + if ($this->hasExtraWithSums()) { + foreach ($this->getExtraWithSums() as $relation => $column) + { + $builder->withSum($relation, $column); + } } if ($this->hasExtraWithCounts()) { - $this->setBuilder($this->getBuilder()->withCount($this->getExtraWithCounts())); + $builder->withCount($this->getExtraWithCounts()); } + + $this->setBuilder($builder); + return $this->getBuilder(); diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index c119b069c..efe3baee9 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -13,7 +13,7 @@ class CountColumn extends Column CountColumnHelpers, CountColumnConfiguration { CountColumnConfiguration::sortable insteadof IsColumn; } - public ?string $countSource; + public ?string $dataSource; public function __construct(string $title, ?string $from = null) { diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 0175a5f1a..487ba6da4 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -1,27 +1,29 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/CountColumnConfiguration.php b/src/Views/Traits/Configuration/CountColumnConfiguration.php index 3aabcd284..dcbe222a4 100644 --- a/src/Views/Traits/Configuration/CountColumnConfiguration.php +++ b/src/Views/Traits/Configuration/CountColumnConfiguration.php @@ -7,10 +7,10 @@ trait CountColumnConfiguration { - public function setCountSource(string $countSource): self + public function setDataSource(string $dataSource): self { - $this->countSource = $countSource; - $this->label(fn ($row, Column $column) => $row->{$countSource.'_count'}); + $this->dataSource = $dataSource; + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_count'}); return $this; } @@ -18,7 +18,7 @@ public function setCountSource(string $countSource): self public function sortable(?callable $callback = null): self { $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->countSource.'_count', $direction) : $callback; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->dataSource.'_count', $direction) : $callback; return $this; } diff --git a/src/Views/Traits/Configuration/SumColumnConfiguration.php b/src/Views/Traits/Configuration/SumColumnConfiguration.php index 24ddd223f..0ba97a635 100644 --- a/src/Views/Traits/Configuration/SumColumnConfiguration.php +++ b/src/Views/Traits/Configuration/SumColumnConfiguration.php @@ -7,10 +7,13 @@ trait SumColumnConfiguration { - public function setDataSource(string $dataSource): self + public function setDataSource(string $dataSource, string $sumColumn): self { + $this->sumColumn = $sumColumn; + $this->dataSource = $dataSource; - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()}); + + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()."_".$sumColumn}); return $this; } @@ -25,7 +28,7 @@ public function setAggregateMethod(string $aggregateMethod): self public function sortable(?callable $callback = null): self { $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod(), $direction) : $callback; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod()."_".$this->getSumColumn(), $direction) : $callback; return $this; } diff --git a/src/Views/Traits/Helpers/CountColumnHelpers.php b/src/Views/Traits/Helpers/CountColumnHelpers.php index 7f10d44e3..d4354daac 100644 --- a/src/Views/Traits/Helpers/CountColumnHelpers.php +++ b/src/Views/Traits/Helpers/CountColumnHelpers.php @@ -7,14 +7,14 @@ trait CountColumnHelpers { - public function getCountSource(): string + public function getDataSource(): string { - return $this->countSource; + return $this->dataSource; } - public function hasCountSource(): bool + public function hasDataSource(): bool { - return isset($this->countSource); + return isset($this->dataSource); } } diff --git a/src/Views/Traits/Helpers/SumColumnHelpers.php b/src/Views/Traits/Helpers/SumColumnHelpers.php index 9c97576dd..f13bcd160 100644 --- a/src/Views/Traits/Helpers/SumColumnHelpers.php +++ b/src/Views/Traits/Helpers/SumColumnHelpers.php @@ -22,4 +22,10 @@ public function getAggregateMethod(): string { return $this->aggregateMethod; } + + public function getSumColumn(): string + { + return $this->sumColumn; + } + } From 775d76093038110b02f99e57d40c141ed8c079df Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 21:45:09 +0000 Subject: [PATCH 15/66] Fix styling --- src/Traits/ComponentUtilities.php | 2 +- .../Configuration/ComponentConfiguration.php | 1 - src/Traits/Helpers/ComponentHelpers.php | 2 - src/Traits/WithData.php | 6 +- src/Views/Columns/SumColumn.php | 58 +++++++++---------- .../Configuration/SumColumnConfiguration.php | 4 +- src/Views/Traits/Helpers/SumColumnHelpers.php | 1 - 7 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 9a5e5e985..d3d48ebd0 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -40,7 +40,7 @@ trait ComponentUtilities protected array $extraWiths = []; protected array $extraWithCounts = []; - + protected array $extraWithSums = []; /** diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index ac02a72b7..79500f629 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -145,5 +145,4 @@ public function addExtraWithSum(string $relationship, string $column): self return $this; } - } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 422c978d8..81eb6611f 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -179,6 +179,4 @@ public function getExtraWithSums(): array { return $this->extraWithSums; } - - } diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 92685c0c2..6f8e80374 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -63,8 +63,7 @@ protected function baseQuery(): Builder } if ($this->hasExtraWithSums()) { - foreach ($this->getExtraWithSums() as $relation => $column) - { + foreach ($this->getExtraWithSums() as $relation => $column) { $builder->withSum($relation, $column); } } @@ -72,9 +71,8 @@ protected function baseQuery(): Builder if ($this->hasExtraWithCounts()) { $builder->withCount($this->getExtraWithCounts()); } - - $this->setBuilder($builder); + $this->setBuilder($builder); return $this->getBuilder(); diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 487ba6da4..ba6e7b566 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -1,29 +1,29 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} diff --git a/src/Views/Traits/Configuration/SumColumnConfiguration.php b/src/Views/Traits/Configuration/SumColumnConfiguration.php index 0ba97a635..3541bc3d2 100644 --- a/src/Views/Traits/Configuration/SumColumnConfiguration.php +++ b/src/Views/Traits/Configuration/SumColumnConfiguration.php @@ -13,7 +13,7 @@ public function setDataSource(string $dataSource, string $sumColumn): self $this->dataSource = $dataSource; - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()."_".$sumColumn}); + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod().'_'.$sumColumn}); return $this; } @@ -28,7 +28,7 @@ public function setAggregateMethod(string $aggregateMethod): self public function sortable(?callable $callback = null): self { $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod()."_".$this->getSumColumn(), $direction) : $callback; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getSumColumn(), $direction) : $callback; return $this; } diff --git a/src/Views/Traits/Helpers/SumColumnHelpers.php b/src/Views/Traits/Helpers/SumColumnHelpers.php index f13bcd160..2a64d3f6d 100644 --- a/src/Views/Traits/Helpers/SumColumnHelpers.php +++ b/src/Views/Traits/Helpers/SumColumnHelpers.php @@ -27,5 +27,4 @@ public function getSumColumn(): string { return $this->sumColumn; } - } From cd2ad2c6a361fd9942b9bea003dc346e1f9ed0ac Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:56:22 +0100 Subject: [PATCH 16/66] Adjustments --- src/Views/Columns/AggregateColumn.php | 3 ++ .../AggregateColumnConfiguration.php | 36 +++++++++++++++++-- .../Configuration/SumColumnConfiguration.php | 8 ++--- .../Traits/Helpers/AggregateColumnHelpers.php | 11 ++++++ src/Views/Traits/Helpers/SumColumnHelpers.php | 9 +++-- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 7007bc4ec..5210a4ad2 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -16,6 +16,9 @@ class AggregateColumn extends Column public ?string $dataSource; public string $aggregateMethod = 'count'; + + public ?string $foreignColumn; + public function __construct(string $title, ?string $from = null) { diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php index 95a1f4161..c9bb8eff1 100644 --- a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -7,10 +7,16 @@ trait AggregateColumnConfiguration { - public function setDataSource(string $dataSource): self + public function setDataSource(string $dataSource, ?string $foreignColumn): self { + if (isset($foreignColumn)) + { + $this->foreignColumn = $foreignColumn; + } + $this->dataSource = $dataSource; - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod()}); + + $this->setDefaultLabel(); return $this; } @@ -18,15 +24,39 @@ public function setDataSource(string $dataSource): self public function setAggregateMethod(string $aggregateMethod): self { $this->aggregateMethod = $aggregateMethod; + $this->setDefaultLabel(); + + return $this; + } + + public function setForeignColumn(string $foreignColumn): self + { + $this->foreignColumn = $foreignColumn; + $this->setDefaultLabel(); return $this; } + public function setDefaultLabel() + { + $this->label(function ($row, Column $column) + { + if ($this->hasForeignColumn()) + { + return $row->{$this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getForeignColumn()}; + } + return $row->{$this->getDataSource().'_'.$this->getAggregateMethod()}; + }); + + } + public function sortable(?callable $callback = null): self { $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod(), $direction) : $callback; + $this->sortCallback = ($callback === null) ? ($this->hasForeignColumn() ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getForeignColumn(), $direction) : fn (Builder $query, string $direction) => $query->orderBy($this->dataSource.'_count', $direction)) : $callback; + + return $this; } } diff --git a/src/Views/Traits/Configuration/SumColumnConfiguration.php b/src/Views/Traits/Configuration/SumColumnConfiguration.php index 3541bc3d2..d5809accb 100644 --- a/src/Views/Traits/Configuration/SumColumnConfiguration.php +++ b/src/Views/Traits/Configuration/SumColumnConfiguration.php @@ -7,13 +7,13 @@ trait SumColumnConfiguration { - public function setDataSource(string $dataSource, string $sumColumn): self + public function setDataSource(string $dataSource, string $foreignColumn): self { - $this->sumColumn = $sumColumn; + $this->foreignColumn = $foreignColumn; $this->dataSource = $dataSource; - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod().'_'.$sumColumn}); + $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod().'_'.$foreignColumn}); return $this; } @@ -28,7 +28,7 @@ public function setAggregateMethod(string $aggregateMethod): self public function sortable(?callable $callback = null): self { $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getSumColumn(), $direction) : $callback; + $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getForeignColumn(), $direction) : $callback; return $this; } diff --git a/src/Views/Traits/Helpers/AggregateColumnHelpers.php b/src/Views/Traits/Helpers/AggregateColumnHelpers.php index 2bd77e983..69fe198f0 100644 --- a/src/Views/Traits/Helpers/AggregateColumnHelpers.php +++ b/src/Views/Traits/Helpers/AggregateColumnHelpers.php @@ -22,4 +22,15 @@ public function getAggregateMethod(): string { return $this->aggregateMethod; } + + public function hasForeignColumn(): bool + { + return isset($this->foreignColumn); + } + + public function getForeignColumn(): string + { + return $this->foreignColumn; + } + } diff --git a/src/Views/Traits/Helpers/SumColumnHelpers.php b/src/Views/Traits/Helpers/SumColumnHelpers.php index 2a64d3f6d..e8307fa36 100644 --- a/src/Views/Traits/Helpers/SumColumnHelpers.php +++ b/src/Views/Traits/Helpers/SumColumnHelpers.php @@ -23,8 +23,13 @@ public function getAggregateMethod(): string return $this->aggregateMethod; } - public function getSumColumn(): string + public function hasForeignColumn(): bool { - return $this->sumColumn; + return isset($this->foreignColumn); + } + + public function getForeignColumn(): string + { + return $this->foreignColumn; } } From 8e26603f47792573b9c20291d6b93da0d21f4503 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 21:56:43 +0000 Subject: [PATCH 17/66] Fix styling --- src/Views/Columns/AggregateColumn.php | 3 +-- .../Configuration/AggregateColumnConfiguration.php | 11 ++++------- src/Views/Traits/Helpers/AggregateColumnHelpers.php | 1 - 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 5210a4ad2..295027a7c 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -16,9 +16,8 @@ class AggregateColumn extends Column public ?string $dataSource; public string $aggregateMethod = 'count'; - - public ?string $foreignColumn; + public ?string $foreignColumn; public function __construct(string $title, ?string $from = null) { diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php index c9bb8eff1..44b5d3e35 100644 --- a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -9,8 +9,7 @@ trait AggregateColumnConfiguration { public function setDataSource(string $dataSource, ?string $foreignColumn): self { - if (isset($foreignColumn)) - { + if (isset($foreignColumn)) { $this->foreignColumn = $foreignColumn; } @@ -39,12 +38,11 @@ public function setForeignColumn(string $foreignColumn): self public function setDefaultLabel() { - $this->label(function ($row, Column $column) - { - if ($this->hasForeignColumn()) - { + $this->label(function ($row, Column $column) { + if ($this->hasForeignColumn()) { return $row->{$this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getForeignColumn()}; } + return $row->{$this->getDataSource().'_'.$this->getAggregateMethod()}; }); @@ -56,7 +54,6 @@ public function sortable(?callable $callback = null): self $this->sortCallback = ($callback === null) ? ($this->hasForeignColumn() ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getForeignColumn(), $direction) : fn (Builder $query, string $direction) => $query->orderBy($this->dataSource.'_count', $direction)) : $callback; - return $this; } } diff --git a/src/Views/Traits/Helpers/AggregateColumnHelpers.php b/src/Views/Traits/Helpers/AggregateColumnHelpers.php index 69fe198f0..4be7f63b1 100644 --- a/src/Views/Traits/Helpers/AggregateColumnHelpers.php +++ b/src/Views/Traits/Helpers/AggregateColumnHelpers.php @@ -32,5 +32,4 @@ public function getForeignColumn(): string { return $this->foreignColumn; } - } From c242a94d7aabd5a9b3fb868e5f2847626d0166bd Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:59:02 +0100 Subject: [PATCH 18/66] Adjust SumColumn --- src/Views/Columns/SumColumn.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index ba6e7b566..ef3bc9d73 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -3,27 +3,20 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\SumColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\SumColumnHelpers; +use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; +use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; -class SumColumn extends Column +class SumColumn extends AggregateColumn { use IsColumn, - SumColumnHelpers, - SumColumnConfiguration { SumColumnConfiguration::sortable insteadof IsColumn; } - - public ?string $dataSource; - - public ?string $sumColumn; + AggregateColumnHelpers, + AggregateColumnConfiguration { AggregateColumnConfiguration::sortable insteadof IsColumn; } public string $aggregateMethod = 'sum'; public function __construct(string $title, ?string $from = null) { parent::__construct($title, $from); - if (! isset($from)) { - $this->label(fn () => null); - } } } From f717b4ffe9c45b7a703db7af2240ac30fdddfc97 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:06:52 +0100 Subject: [PATCH 19/66] Add fixes --- src/Traits/Helpers/ColumnHelpers.php | 8 ++--- .../AggregateColumnConfiguration.php | 2 +- .../Configuration/SumColumnConfiguration.php | 35 ------------------- src/Views/Traits/Helpers/SumColumnHelpers.php | 35 ------------------- 4 files changed, 5 insertions(+), 75 deletions(-) delete mode 100644 src/Views/Traits/Configuration/SumColumnConfiguration.php delete mode 100644 src/Views/Traits/Helpers/SumColumnHelpers.php diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index 61ef60fa2..905d62e36 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -4,7 +4,7 @@ use Illuminate\Support\Collection; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Columns\{CountColumn,SumColumn}; +use Rappasoft\LaravelLivewireTables\Views\Columns\{AggregateColumn,CountColumn,SumColumn}; trait ColumnHelpers { @@ -27,7 +27,7 @@ public function setColumns(): void if ($column instanceof SumColumn) { if ($column->hasDataSource()) { - $this->addExtraWithSum($column->getDataSource(), $column->getSumColumn()); + $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } @@ -218,7 +218,7 @@ public function getPrependedColumns(): Collection if ($column instanceof SumColumn) { if ($column->hasDataSource()) { - $this->addExtraWithSum($column->getDataSource(), $column->getSumColumn()); + $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } @@ -248,7 +248,7 @@ public function getAppendedColumns(): Collection if ($column instanceof SumColumn) { if ($column->hasDataSource()) { - $this->addExtraWithSum($column->getDataSource(), $column->getSumColumn()); + $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php index 44b5d3e35..28b93f940 100644 --- a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -7,7 +7,7 @@ trait AggregateColumnConfiguration { - public function setDataSource(string $dataSource, ?string $foreignColumn): self + public function setDataSource(string $dataSource, ?string $foreignColumn = null): self { if (isset($foreignColumn)) { $this->foreignColumn = $foreignColumn; diff --git a/src/Views/Traits/Configuration/SumColumnConfiguration.php b/src/Views/Traits/Configuration/SumColumnConfiguration.php deleted file mode 100644 index d5809accb..000000000 --- a/src/Views/Traits/Configuration/SumColumnConfiguration.php +++ /dev/null @@ -1,35 +0,0 @@ -foreignColumn = $foreignColumn; - - $this->dataSource = $dataSource; - - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_'.$this->getAggregateMethod().'_'.$foreignColumn}); - - return $this; - } - - public function setAggregateMethod(string $aggregateMethod): self - { - $this->aggregateMethod = $aggregateMethod; - - return $this; - } - - public function sortable(?callable $callback = null): self - { - $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->getDataSource().'_'.$this->getAggregateMethod().'_'.$this->getForeignColumn(), $direction) : $callback; - - return $this; - } -} diff --git a/src/Views/Traits/Helpers/SumColumnHelpers.php b/src/Views/Traits/Helpers/SumColumnHelpers.php deleted file mode 100644 index e8307fa36..000000000 --- a/src/Views/Traits/Helpers/SumColumnHelpers.php +++ /dev/null @@ -1,35 +0,0 @@ -dataSource; - } - - public function hasDataSource(): bool - { - return isset($this->dataSource); - } - - public function getAggregateMethod(): string - { - return $this->aggregateMethod; - } - - public function hasForeignColumn(): bool - { - return isset($this->foreignColumn); - } - - public function getForeignColumn(): string - { - return $this->foreignColumn; - } -} From c776bdbfdf80386a96377f0c72398dbb42297e27 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:07:42 +0100 Subject: [PATCH 20/66] Change CountColumn to extend AggregateColumn --- src/Views/Columns/CountColumn.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index efe3baee9..65b3f1a30 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -3,17 +3,17 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\CountColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\CountColumnHelpers; +use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; +use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; -class CountColumn extends Column +class CountColumn extends AggregateColumn { use IsColumn, - CountColumnHelpers, - CountColumnConfiguration { CountColumnConfiguration::sortable insteadof IsColumn; } + AggregateColumnHelpers, + AggregateColumnConfiguration { AggregateColumnConfiguration::sortable insteadof IsColumn; } - public ?string $dataSource; + public string $aggregateMethod = 'count'; public function __construct(string $title, ?string $from = null) { From 91c8b44a1401e7eb898a7853342ae7dd69159738 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:11:12 +0100 Subject: [PATCH 21/66] Clean up Helpers --- src/Traits/Helpers/ColumnHelpers.php | 34 ++++++++----------- .../CountColumnConfiguration.php | 25 -------------- .../Traits/Helpers/CountColumnHelpers.php | 20 ----------- 3 files changed, 15 insertions(+), 64 deletions(-) delete mode 100644 src/Views/Traits/Configuration/CountColumnConfiguration.php delete mode 100644 src/Views/Traits/Helpers/CountColumnHelpers.php diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index 905d62e36..8d8029228 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -4,7 +4,7 @@ use Illuminate\Support\Collection; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Columns\{AggregateColumn,CountColumn,SumColumn}; +use Rappasoft\LaravelLivewireTables\Views\Columns\AggregateColumn; trait ColumnHelpers { @@ -19,14 +19,12 @@ public function setColumns(): void ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); - if ($column instanceof CountColumn) { - if ($column->hasDataSource()) { + if ($column instanceof AggregateColumn) { + if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) { $this->addExtraWithCount($column->getDataSource()); } - } - - if ($column instanceof SumColumn) { - if ($column->hasDataSource()) { + elseif($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) + { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } @@ -210,18 +208,17 @@ public function getPrependedColumns(): Collection ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); - if ($column instanceof CountColumn) { - if ($column->hasDataSource()) { + if ($column instanceof AggregateColumn) { + if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) { $this->addExtraWithCount($column->getDataSource()); } - } - - if ($column instanceof SumColumn) { - if ($column->hasDataSource()) { + elseif($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) + { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } + if ($column->hasField()) { if ($column->isBaseColumn()) { $column->setTable($this->getBuilder()->getModel()->getTable()); @@ -240,18 +237,17 @@ public function getAppendedColumns(): Collection ->filter(fn ($column) => $column instanceof Column) ->map(function (Column $column) { $column->setComponent($this); - if ($column instanceof CountColumn) { - if ($column->hasDataSource()) { + if ($column instanceof AggregateColumn) { + if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) { $this->addExtraWithCount($column->getDataSource()); } - } - - if ($column instanceof SumColumn) { - if ($column->hasDataSource()) { + elseif($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) + { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } + if ($column->hasField()) { if ($column->isBaseColumn()) { $column->setTable($this->getBuilder()->getModel()->getTable()); diff --git a/src/Views/Traits/Configuration/CountColumnConfiguration.php b/src/Views/Traits/Configuration/CountColumnConfiguration.php deleted file mode 100644 index dcbe222a4..000000000 --- a/src/Views/Traits/Configuration/CountColumnConfiguration.php +++ /dev/null @@ -1,25 +0,0 @@ -dataSource = $dataSource; - $this->label(fn ($row, Column $column) => $row->{$dataSource.'_count'}); - - return $this; - } - - public function sortable(?callable $callback = null): self - { - $this->sortable = true; - $this->sortCallback = ($callback === null) ? fn (Builder $query, string $direction) => $query->orderBy($this->dataSource.'_count', $direction) : $callback; - - return $this; - } -} diff --git a/src/Views/Traits/Helpers/CountColumnHelpers.php b/src/Views/Traits/Helpers/CountColumnHelpers.php deleted file mode 100644 index d4354daac..000000000 --- a/src/Views/Traits/Helpers/CountColumnHelpers.php +++ /dev/null @@ -1,20 +0,0 @@ -dataSource; - } - - public function hasDataSource(): bool - { - return isset($this->dataSource); - } -} From a191aa459fbb339316c2894e08fe1affb5013c3d Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 22:11:39 +0000 Subject: [PATCH 22/66] Fix styling --- src/Traits/Helpers/ColumnHelpers.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index 8d8029228..a7c33062d 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -22,9 +22,7 @@ public function setColumns(): void if ($column instanceof AggregateColumn) { if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) { $this->addExtraWithCount($column->getDataSource()); - } - elseif($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) - { + } elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } @@ -211,14 +209,11 @@ public function getPrependedColumns(): Collection if ($column instanceof AggregateColumn) { if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) { $this->addExtraWithCount($column->getDataSource()); - } - elseif($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) - { + } elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } - if ($column->hasField()) { if ($column->isBaseColumn()) { $column->setTable($this->getBuilder()->getModel()->getTable()); @@ -240,14 +235,11 @@ public function getAppendedColumns(): Collection if ($column instanceof AggregateColumn) { if ($column->getAggregateMethod() == 'count' && $column->hasDataSource()) { $this->addExtraWithCount($column->getDataSource()); - } - elseif($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) - { + } elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); } } - if ($column->hasField()) { if ($column->isBaseColumn()) { $column->setTable($this->getBuilder()->getModel()->getTable()); From 9a5dbe49d420bc56351eff16127537df8a235d60 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:33:22 +0100 Subject: [PATCH 23/66] AvgCol --- src/Traits/ComponentUtilities.php | 2 ++ .../Configuration/ComponentConfiguration.php | 8 ++++++ src/Traits/Helpers/ColumnHelpers.php | 6 +++++ src/Traits/Helpers/ComponentHelpers.php | 11 ++++++++ src/Views/Columns/AvgColumn.php | 25 +++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 src/Views/Columns/AvgColumn.php diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index d3d48ebd0..8ec4bd8b6 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -43,6 +43,8 @@ trait ComponentUtilities protected array $extraWithSums = []; + protected array $extraWithAvgs = []; + /** * Set any configuration options */ diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 79500f629..100576f97 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -145,4 +145,12 @@ public function addExtraWithSum(string $relationship, string $column): self return $this; } + + public function addExtraWithAvg(string $extraWithAvg): self + { + $this->extraWithAvgs[] = $extraWithAvg; + + return $this; + } + } diff --git a/src/Traits/Helpers/ColumnHelpers.php b/src/Traits/Helpers/ColumnHelpers.php index a7c33062d..920d63fe6 100644 --- a/src/Traits/Helpers/ColumnHelpers.php +++ b/src/Traits/Helpers/ColumnHelpers.php @@ -24,6 +24,8 @@ public function setColumns(): void $this->addExtraWithCount($column->getDataSource()); } elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); + } elseif ($column->getAggregateMethod() == 'avg' && $column->hasDataSource() && $column->hasForeignColumn()) { + $this->addExtraWithAvg($column->getDataSource(), $column->getForeignColumn()); } } @@ -211,6 +213,8 @@ public function getPrependedColumns(): Collection $this->addExtraWithCount($column->getDataSource()); } elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); + } elseif ($column->getAggregateMethod() == 'avg' && $column->hasDataSource() && $column->hasForeignColumn()) { + $this->addExtraWithAvg($column->getDataSource(), $column->getForeignColumn()); } } @@ -237,6 +241,8 @@ public function getAppendedColumns(): Collection $this->addExtraWithCount($column->getDataSource()); } elseif ($column->getAggregateMethod() == 'sum' && $column->hasDataSource() && $column->hasForeignColumn()) { $this->addExtraWithSum($column->getDataSource(), $column->getForeignColumn()); + } elseif ($column->getAggregateMethod() == 'avg' && $column->hasDataSource() && $column->hasForeignColumn()) { + $this->addExtraWithAvg($column->getDataSource(), $column->getForeignColumn()); } } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 81eb6611f..82704f0b5 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -179,4 +179,15 @@ public function getExtraWithSums(): array { return $this->extraWithSums; } + + public function hasExtraWithAvgs(): bool + { + return ! empty($this->extraWithAvgs); + } + + public function getExtraWithAvgs(): array + { + return $this->extraWithAvgs; + } + } diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php new file mode 100644 index 000000000..12a4e7f68 --- /dev/null +++ b/src/Views/Columns/AvgColumn.php @@ -0,0 +1,25 @@ +label(fn () => null); + } + } +} From b64e5270c2e997ba4aac6531191c1333831beab9 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 22:33:44 +0000 Subject: [PATCH 24/66] Fix styling --- .../Configuration/ComponentConfiguration.php | 1 - src/Traits/Helpers/ComponentHelpers.php | 1 - src/Views/Columns/AvgColumn.php | 50 +++++++++---------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 100576f97..09b6adb94 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -152,5 +152,4 @@ public function addExtraWithAvg(string $extraWithAvg): self return $this; } - } diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index 82704f0b5..96eae35f8 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -189,5 +189,4 @@ public function getExtraWithAvgs(): array { return $this->extraWithAvgs; } - } diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index 12a4e7f68..26c69954a 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -1,25 +1,25 @@ -label(fn () => null); - } - } -} +label(fn () => null); + } + } +} From 0e500f172f09c293aaa85a317e3851eaf0137a99 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:40:58 +0100 Subject: [PATCH 25/66] Adjust Avg/Sum --- src/Traits/Configuration/ComponentConfiguration.php | 8 ++++---- src/Traits/WithData.php | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 09b6adb94..239ca2a41 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -141,15 +141,15 @@ public function addExtraWithCounts(array $extraWithCounts): self public function addExtraWithSum(string $relationship, string $column): self { - $this->extraWithSums[$relationship] = $column; + $this->extraWithSums[] = ['table' => $relationship, 'field' => $column]; return $this; } - - public function addExtraWithAvg(string $extraWithAvg): self + public function addExtraWithAvg(string $relationship, string $column): self { - $this->extraWithAvgs[] = $extraWithAvg; + $this->extraWithAvgs[] = ['table' => $relationship, 'field' => $column]; return $this; } + } diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index 6f8e80374..23d1055bb 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -63,8 +63,13 @@ protected function baseQuery(): Builder } if ($this->hasExtraWithSums()) { - foreach ($this->getExtraWithSums() as $relation => $column) { - $builder->withSum($relation, $column); + foreach ($this->getExtraWithSums() as $extraSum) { + $builder->withSum($extraSum['table'], $extraSum['field']); + } + } + if ($this->hasExtraWithAvgs()) { + foreach ($this->getExtraWithAvgs() as $extraAvg) { + $builder->withAvg($extraAvg['table'], $extraAvg['field']); } } From 27eb5618a3966ebfdb12547a2f985e0d7416d607 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Fri, 5 Jul 2024 22:41:23 +0000 Subject: [PATCH 26/66] Fix styling --- src/Traits/Configuration/ComponentConfiguration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index 239ca2a41..da4ea2a24 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -145,11 +145,11 @@ public function addExtraWithSum(string $relationship, string $column): self return $this; } + public function addExtraWithAvg(string $relationship, string $column): self { $this->extraWithAvgs[] = ['table' => $relationship, 'field' => $column]; return $this; } - } From d31fdf0119ae55ef693205650cfbf586f52a668c Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:43:15 +0100 Subject: [PATCH 27/66] Start docs --- docs/columns/other-column-types.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/columns/other-column-types.md b/docs/columns/other-column-types.md index dc80da449..a8630a0e2 100644 --- a/docs/columns/other-column-types.md +++ b/docs/columns/other-column-types.md @@ -247,3 +247,11 @@ ComponentColumn::make('E-mail', 'email') 'dismissible' => true, ]), ``` + +## Aggregate Columns + +### AvgColumn + +### CountColumn + +### SumColumn \ No newline at end of file From 31120f2d30e0c2b2c51ceb5554bf8e82d802b07e Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:51:50 +0100 Subject: [PATCH 28/66] Update Docs - Add Column Types Section --- docs/bulk-actions/_index.md | 2 +- docs/column-types/_index.md | 4 ++++ docs/column-types/avg_column.md | 4 ++++ docs/column-types/boolean_columns.md | 4 ++++ docs/column-types/button_group_column.md | 4 ++++ docs/column-types/color_columns.md | 4 ++++ docs/column-types/component_column.md | 4 ++++ docs/column-types/count_column.md | 4 ++++ docs/column-types/date_columns.md | 4 ++++ docs/column-types/image_columns.md | 4 ++++ docs/column-types/link_columns.md | 4 ++++ docs/column-types/sum_column.md | 4 ++++ docs/examples/_index.md | 2 +- docs/filter-types/_index.md | 2 +- docs/filters/_index.md | 2 +- docs/footer/_index.md | 2 +- docs/misc/_index.md | 2 +- docs/pagination/_index.md | 2 +- docs/reordering/_index.md | 2 +- docs/rows/_index.md | 2 +- docs/search/_index.md | 2 +- docs/secondary-header/_index.md | 2 +- docs/sorting/_index.md | 2 +- 23 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 docs/column-types/_index.md create mode 100644 docs/column-types/avg_column.md create mode 100644 docs/column-types/boolean_columns.md create mode 100644 docs/column-types/button_group_column.md create mode 100644 docs/column-types/color_columns.md create mode 100644 docs/column-types/component_column.md create mode 100644 docs/column-types/count_column.md create mode 100644 docs/column-types/date_columns.md create mode 100644 docs/column-types/image_columns.md create mode 100644 docs/column-types/link_columns.md create mode 100644 docs/column-types/sum_column.md diff --git a/docs/bulk-actions/_index.md b/docs/bulk-actions/_index.md index f76ab3244..48cb71a17 100644 --- a/docs/bulk-actions/_index.md +++ b/docs/bulk-actions/_index.md @@ -1,4 +1,4 @@ --- title: Bulk Actions -weight: 9 +weight: 10 --- diff --git a/docs/column-types/_index.md b/docs/column-types/_index.md new file mode 100644 index 000000000..821c869a2 --- /dev/null +++ b/docs/column-types/_index.md @@ -0,0 +1,4 @@ +--- +title: Column Types +weight: 5 +--- diff --git a/docs/column-types/avg_column.md b/docs/column-types/avg_column.md new file mode 100644 index 000000000..54e38e48f --- /dev/null +++ b/docs/column-types/avg_column.md @@ -0,0 +1,4 @@ +--- +title: Avg Columns +weight: 4 +--- diff --git a/docs/column-types/boolean_columns.md b/docs/column-types/boolean_columns.md new file mode 100644 index 000000000..f0c94e8aa --- /dev/null +++ b/docs/column-types/boolean_columns.md @@ -0,0 +1,4 @@ +--- +title: Boolean Columns +weight: 4 +--- diff --git a/docs/column-types/button_group_column.md b/docs/column-types/button_group_column.md new file mode 100644 index 000000000..145e7fcda --- /dev/null +++ b/docs/column-types/button_group_column.md @@ -0,0 +1,4 @@ +--- +title: Button Group Columns +weight: 4 +--- diff --git a/docs/column-types/color_columns.md b/docs/column-types/color_columns.md new file mode 100644 index 000000000..1e5400cf4 --- /dev/null +++ b/docs/column-types/color_columns.md @@ -0,0 +1,4 @@ +--- +title: Color Columns +weight: 4 +--- diff --git a/docs/column-types/component_column.md b/docs/column-types/component_column.md new file mode 100644 index 000000000..2cb8cfa17 --- /dev/null +++ b/docs/column-types/component_column.md @@ -0,0 +1,4 @@ +--- +title: Component Columns +weight: 4 +--- diff --git a/docs/column-types/count_column.md b/docs/column-types/count_column.md new file mode 100644 index 000000000..79a8fb7dc --- /dev/null +++ b/docs/column-types/count_column.md @@ -0,0 +1,4 @@ +--- +title: Count Columns +weight: 4 +--- diff --git a/docs/column-types/date_columns.md b/docs/column-types/date_columns.md new file mode 100644 index 000000000..41aa7a760 --- /dev/null +++ b/docs/column-types/date_columns.md @@ -0,0 +1,4 @@ +--- +title: Date Columns +weight: 4 +--- diff --git a/docs/column-types/image_columns.md b/docs/column-types/image_columns.md new file mode 100644 index 000000000..4f30c76b6 --- /dev/null +++ b/docs/column-types/image_columns.md @@ -0,0 +1,4 @@ +--- +title: Image Columns +weight: 4 +--- diff --git a/docs/column-types/link_columns.md b/docs/column-types/link_columns.md new file mode 100644 index 000000000..b0101cd61 --- /dev/null +++ b/docs/column-types/link_columns.md @@ -0,0 +1,4 @@ +--- +title: Link Columns +weight: 4 +--- diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md new file mode 100644 index 000000000..05bba3278 --- /dev/null +++ b/docs/column-types/sum_column.md @@ -0,0 +1,4 @@ +--- +title: Sum Columns +weight: 4 +--- diff --git a/docs/examples/_index.md b/docs/examples/_index.md index a831d1a26..03b1d982b 100644 --- a/docs/examples/_index.md +++ b/docs/examples/_index.md @@ -1,4 +1,4 @@ --- title: Examples -weight: 14 +weight: 16 --- diff --git a/docs/filter-types/_index.md b/docs/filter-types/_index.md index 6c6d8ccf4..3631183ba 100644 --- a/docs/filter-types/_index.md +++ b/docs/filter-types/_index.md @@ -1,4 +1,4 @@ --- title: Filter Types -weight: 11 +weight: 12 --- diff --git a/docs/filters/_index.md b/docs/filters/_index.md index 43af373bd..e0ac299f1 100644 --- a/docs/filters/_index.md +++ b/docs/filters/_index.md @@ -1,4 +1,4 @@ --- title: Filters -weight: 10 +weight: 11 --- diff --git a/docs/footer/_index.md b/docs/footer/_index.md index e16f7efb5..4edcb2460 100644 --- a/docs/footer/_index.md +++ b/docs/footer/_index.md @@ -1,4 +1,4 @@ --- title: Footer -weight: 13 +weight: 15 --- diff --git a/docs/misc/_index.md b/docs/misc/_index.md index 9f806b32e..193fabdf4 100644 --- a/docs/misc/_index.md +++ b/docs/misc/_index.md @@ -1,4 +1,4 @@ --- title: Misc. -weight: 15 +weight: 17 --- diff --git a/docs/pagination/_index.md b/docs/pagination/_index.md index 723cb0c11..50491640d 100644 --- a/docs/pagination/_index.md +++ b/docs/pagination/_index.md @@ -1,4 +1,4 @@ --- title: Pagination -weight: 7 +weight: 8 --- diff --git a/docs/reordering/_index.md b/docs/reordering/_index.md index bc21a450b..e66081c28 100644 --- a/docs/reordering/_index.md +++ b/docs/reordering/_index.md @@ -1,4 +1,4 @@ --- title: Reordering -weight: 11 +weight: 13 --- diff --git a/docs/rows/_index.md b/docs/rows/_index.md index f731c19d2..7d5ebba99 100644 --- a/docs/rows/_index.md +++ b/docs/rows/_index.md @@ -1,4 +1,4 @@ --- title: Rows -weight: 5 +weight: 6 --- diff --git a/docs/search/_index.md b/docs/search/_index.md index edb174fde..4aac1542b 100644 --- a/docs/search/_index.md +++ b/docs/search/_index.md @@ -1,4 +1,4 @@ --- title: Search -weight: 8 +weight: 9 --- diff --git a/docs/secondary-header/_index.md b/docs/secondary-header/_index.md index 5e2b9aba5..f334c9e68 100644 --- a/docs/secondary-header/_index.md +++ b/docs/secondary-header/_index.md @@ -1,4 +1,4 @@ --- title: Secondary Header -weight: 12 +weight: 14 --- diff --git a/docs/sorting/_index.md b/docs/sorting/_index.md index a99eabf1d..422739fb6 100644 --- a/docs/sorting/_index.md +++ b/docs/sorting/_index.md @@ -1,4 +1,4 @@ --- title: Sorting -weight: 6 +weight: 7 --- From 8c7363084c36816b5bf3839dd29281efd7dfcf3b Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Fri, 5 Jul 2024 23:53:25 +0100 Subject: [PATCH 29/66] Migrating Column Docs --- docs/column-types/boolean_columns.md | 79 ++++++++ docs/column-types/button_group_column.md | 31 +++ docs/column-types/color_columns.md | 39 ++++ docs/column-types/component_column.md | 25 +++ docs/column-types/date_columns.md | 26 +++ docs/column-types/image_columns.md | 23 +++ docs/column-types/link_columns.md | 21 ++ docs/columns/other-column-types.md | 241 ----------------------- 8 files changed, 244 insertions(+), 241 deletions(-) diff --git a/docs/column-types/boolean_columns.md b/docs/column-types/boolean_columns.md index f0c94e8aa..51efb3bda 100644 --- a/docs/column-types/boolean_columns.md +++ b/docs/column-types/boolean_columns.md @@ -2,3 +2,82 @@ title: Boolean Columns weight: 4 --- + +## Boolean Columns + +Boolean columns are good if you have a column type that is a true/false, or 0/1 value. + +For example: + +```php +BooleanColumn::make('Active') +``` + +Would yield: + +![Boolean Column](https://imgur.com/LAk6gHY.png) + +### Using your own view + +If you don't want to use the default view and icons you can set your own: + +```php +BooleanColumn::make('Active') + ->setView('my.active.view') +``` + +You will have access to `$component`, `$status`, and `$successValue`. + +To help you better understand, this is the Tailwind implementation of BooleanColumn: + +```html +@if ($status) + + + +@else + + + +@endif +``` + +### Setting the truthy value + +If you want the false value to be the green option, you can set: + +```php +BooleanColumn::make('Active') + ->setSuccessValue(false); // Makes false the 'successful' option +``` + +That would swap the colors of the icons in the image above. + +### Setting the status value + +By default, the `$status` is set to: + +```php +(bool)$value === true +``` + +You can override this functionality: + +```php +BooleanColumn::make('Active') + // Note: Parameter `$row` available as of v2.4 + ->setCallback(function(string $value, $row) { + // Figure out what makes $value true + }), +``` + +### Different types of boolean display + +By default, the BooleanColumn displays icons. + +If you would like the BooleanColumn to display a plain Yes/No, you can set: + +```php +BooleanColumn::make('Active') + ->yesNo() +``` diff --git a/docs/column-types/button_group_column.md b/docs/column-types/button_group_column.md index 145e7fcda..aa314c40d 100644 --- a/docs/column-types/button_group_column.md +++ b/docs/column-types/button_group_column.md @@ -2,3 +2,34 @@ title: Button Group Columns weight: 4 --- +## Button Group Columns + +Button group columns let you provide an array of LinkColumns to display in a single cell. + +```php +ButtonGroupColumn::make('Actions') + ->attributes(function($row) { + return [ + 'class' => 'space-x-2', + ]; + }) + ->buttons([ + LinkColumn::make('View') // make() has no effect in this case but needs to be set anyway + ->title(fn($row) => 'View ' . $row->name) + ->location(fn($row) => route('user.show', $row)) + ->attributes(function($row) { + return [ + 'class' => 'underline text-blue-500 hover:no-underline', + ]; + }), + LinkColumn::make('Edit') + ->title(fn($row) => 'Edit ' . $row->name) + ->location(fn($row) => route('user.edit', $row)) + ->attributes(function($row) { + return [ + 'target' => '_blank', + 'class' => 'underline text-blue-500 hover:no-underline', + ]; + }), + ]), +``` diff --git a/docs/column-types/color_columns.md b/docs/column-types/color_columns.md index 1e5400cf4..aa3ae8d9a 100644 --- a/docs/column-types/color_columns.md +++ b/docs/column-types/color_columns.md @@ -2,3 +2,42 @@ title: Color Columns weight: 4 --- + +## Color Columns + +Color columns provide an easy way to a Color in a Column + +You may pass either pass a CSS-compliant colour as a field +```php +ColorColumn::make('Favourite Colour', 'favourite_color'), +``` + +Or you may use a Callback +```php +ColorColumn::make('Favourite Colour') + ->color( + function ($row) { + if ($row->success_rate < 40) + { + return '#ff0000'; + } + else if ($row->success_rate > 90) + { + return '#008000'; + } + else return '#ffa500'; + + } + ), +``` + +You may also specify attributes to use on the div displaying the color, to adjust the size or appearance, this receives the full row. By default, this will replace the standard classes, to retain them, set "default" to true. To then over-ride, you should prefix your classes with "!" to signify importance. +```php + ColorColumn::make('Favourite Colour') + ->attributes(function ($row) { + return [ + 'class' => '!rounded-lg self-center', + 'default' => true, + ]; + }), +``` diff --git a/docs/column-types/component_column.md b/docs/column-types/component_column.md index 2cb8cfa17..77d8cb12d 100644 --- a/docs/column-types/component_column.md +++ b/docs/column-types/component_column.md @@ -2,3 +2,28 @@ title: Component Columns weight: 4 --- +## Component Columns + +Component columns let you specify a component name and attributes and provides the column value to the slot. + +```php +// Before +Column::make("Email", "email") + ->format(function ($value) { + return view('components.alert') + ->with('attributes', new ComponentAttributeBag([ + 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', + 'dismissible' => true, + ])) + ->with('slot', $value); + }), + +// After +ComponentColumn::make('E-mail', 'email') + ->component('email') + ->attributes(fn ($value, $row, Column $column) => [ + 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', + 'dismissible' => true, + ]), +``` + diff --git a/docs/column-types/date_columns.md b/docs/column-types/date_columns.md index 41aa7a760..f177132fb 100644 --- a/docs/column-types/date_columns.md +++ b/docs/column-types/date_columns.md @@ -2,3 +2,29 @@ title: Date Columns weight: 4 --- + +## Date Columns + +Date columns provide an easy way to display dates in a given format, without having to use repetitive format() methods or partial views. + +You may pass either a DateTime object, in which you can define an "outputFormat" +```php +DateColumn::make('Updated At', 'updated_at') + ->outputFormat('Y-m-d H:i:s), +``` + +Or you may pass a string, in which case you can define an "inputFormat" in addition to the outputFormat: +```php +DateColumn::make('Last Charged', 'last_charged_at') + ->inputFormat('Y-m-d H:i:s') + ->outputFormat('Y-m-d'), +``` + +You may also set an "emptyValue" to use when there is no value from the database: +```php +DateColumn::make('Last Charged', 'last_charged_at') + ->inputFormat('Y-m-d H:i:s') + ->outputFormat('Y-m-d') + ->emptyValue('Not Found'), +``` + diff --git a/docs/column-types/image_columns.md b/docs/column-types/image_columns.md index 4f30c76b6..4c3c407c8 100644 --- a/docs/column-types/image_columns.md +++ b/docs/column-types/image_columns.md @@ -2,3 +2,26 @@ title: Image Columns weight: 4 --- +## Image Columns + +Image columns provide a way to display images in your table without having to use `format()` or partial views: + +```php +ImageColumn::make('Avatar') + ->location( + fn($row) => storage_path('app/public/avatars/' . $row->id . '.jpg') + ), +``` + +You may also pass an array of attributes to apply to the image tag: + +```php +ImageColumn::make('Avatar') + ->location( + fn($row) => storage_path('app/public/avatars/' . $row->id . '.jpg') + ) + ->attributes(fn($row) => [ + 'class' => 'rounded-full', + 'alt' => $row->name . ' Avatar', + ]), +``` diff --git a/docs/column-types/link_columns.md b/docs/column-types/link_columns.md index b0101cd61..d1484bdb3 100644 --- a/docs/column-types/link_columns.md +++ b/docs/column-types/link_columns.md @@ -2,3 +2,24 @@ title: Link Columns weight: 4 --- +## Link Columns + +Link columns provide a way to display HTML links in your table without having to use `format()` or partial views: + +```php +LinkColumn::make('Action') + ->title(fn($row) => 'Edit') + ->location(fn($row) => route('admin.users.edit', $row)), +``` + +You may also pass an array of attributes to apply to the `a` tag: + +```php +LinkColumn::make('Action') + ->title(fn($row) => 'Edit') + ->location(fn($row) => route('admin.users.edit', $row)) + ->attributes(fn($row) => [ + 'class' => 'rounded-full', + 'alt' => $row->name . ' Avatar', + ]), +``` diff --git a/docs/columns/other-column-types.md b/docs/columns/other-column-types.md index a8630a0e2..2e515eedf 100644 --- a/docs/columns/other-column-types.md +++ b/docs/columns/other-column-types.md @@ -3,250 +3,9 @@ title: Other Column Types weight: 4 --- -## Boolean Columns -Boolean columns are good if you have a column type that is a true/false, or 0/1 value. -For example: -```php -BooleanColumn::make('Active') -``` - -Would yield: - -![Boolean Column](https://imgur.com/LAk6gHY.png) - -### Using your own view - -If you don't want to use the default view and icons you can set your own: - -```php -BooleanColumn::make('Active') - ->setView('my.active.view') -``` - -You will have access to `$component`, `$status`, and `$successValue`. - -To help you better understand, this is the Tailwind implementation of BooleanColumn: - -```html -@if ($status) - - - -@else - - - -@endif -``` - -### Setting the truthy value - -If you want the false value to be the green option, you can set: - -```php -BooleanColumn::make('Active') - ->setSuccessValue(false); // Makes false the 'successful' option -``` - -That would swap the colors of the icons in the image above. - -### Setting the status value - -By default, the `$status` is set to: - -```php -(bool)$value === true -``` - -You can override this functionality: - -```php -BooleanColumn::make('Active') - // Note: Parameter `$row` available as of v2.4 - ->setCallback(function(string $value, $row) { - // Figure out what makes $value true - }), -``` - -### Different types of boolean display - -By default, the BooleanColumn displays icons. - -If you would like the BooleanColumn to display a plain Yes/No, you can set: - -```php -BooleanColumn::make('Active') - ->yesNo() -``` -## Color Columns - -Color columns provide an easy way to a Color in a Column - -You may pass either pass a CSS-compliant colour as a field -```php -ColorColumn::make('Favourite Colour', 'favourite_color'), -``` - -Or you may use a Callback -```php -ColorColumn::make('Favourite Colour') - ->color( - function ($row) { - if ($row->success_rate < 40) - { - return '#ff0000'; - } - else if ($row->success_rate > 90) - { - return '#008000'; - } - else return '#ffa500'; - - } - ), -``` - -You may also specify attributes to use on the div displaying the color, to adjust the size or appearance, this receives the full row. By default, this will replace the standard classes, to retain them, set "default" to true. To then over-ride, you should prefix your classes with "!" to signify importance. -```php - ColorColumn::make('Favourite Colour') - ->attributes(function ($row) { - return [ - 'class' => '!rounded-lg self-center', - 'default' => true, - ]; - }), -``` - -## Date Columns - -Date columns provide an easy way to display dates in a given format, without having to use repetitive format() methods or partial views. - -You may pass either a DateTime object, in which you can define an "outputFormat" -```php -DateColumn::make('Updated At', 'updated_at') - ->outputFormat('Y-m-d H:i:s), -``` - -Or you may pass a string, in which case you can define an "inputFormat" in addition to the outputFormat: -```php -DateColumn::make('Last Charged', 'last_charged_at') - ->inputFormat('Y-m-d H:i:s') - ->outputFormat('Y-m-d'), -``` - -You may also set an "emptyValue" to use when there is no value from the database: -```php -DateColumn::make('Last Charged', 'last_charged_at') - ->inputFormat('Y-m-d H:i:s') - ->outputFormat('Y-m-d') - ->emptyValue('Not Found'), -``` - -## Image Columns - -Image columns provide a way to display images in your table without having to use `format()` or partial views: - -```php -ImageColumn::make('Avatar') - ->location( - fn($row) => storage_path('app/public/avatars/' . $row->id . '.jpg') - ), -``` - -You may also pass an array of attributes to apply to the image tag: - -```php -ImageColumn::make('Avatar') - ->location( - fn($row) => storage_path('app/public/avatars/' . $row->id . '.jpg') - ) - ->attributes(fn($row) => [ - 'class' => 'rounded-full', - 'alt' => $row->name . ' Avatar', - ]), -``` - -## Link Columns - -Link columns provide a way to display HTML links in your table without having to use `format()` or partial views: - -```php -LinkColumn::make('Action') - ->title(fn($row) => 'Edit') - ->location(fn($row) => route('admin.users.edit', $row)), -``` - -You may also pass an array of attributes to apply to the `a` tag: - -```php -LinkColumn::make('Action') - ->title(fn($row) => 'Edit') - ->location(fn($row) => route('admin.users.edit', $row)) - ->attributes(fn($row) => [ - 'class' => 'rounded-full', - 'alt' => $row->name . ' Avatar', - ]), -``` - -## Button Group Columns - -Button group columns let you provide an array of LinkColumns to display in a single cell. - -```php -ButtonGroupColumn::make('Actions') - ->attributes(function($row) { - return [ - 'class' => 'space-x-2', - ]; - }) - ->buttons([ - LinkColumn::make('View') // make() has no effect in this case but needs to be set anyway - ->title(fn($row) => 'View ' . $row->name) - ->location(fn($row) => route('user.show', $row)) - ->attributes(function($row) { - return [ - 'class' => 'underline text-blue-500 hover:no-underline', - ]; - }), - LinkColumn::make('Edit') - ->title(fn($row) => 'Edit ' . $row->name) - ->location(fn($row) => route('user.edit', $row)) - ->attributes(function($row) { - return [ - 'target' => '_blank', - 'class' => 'underline text-blue-500 hover:no-underline', - ]; - }), - ]), -``` - -## Component Columns - -Component columns let you specify a component name and attributes and provides the column value to the slot. - -```php -// Before -Column::make("Email", "email") - ->format(function ($value) { - return view('components.alert') - ->with('attributes', new ComponentAttributeBag([ - 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', - 'dismissible' => true, - ])) - ->with('slot', $value); - }), - -// After -ComponentColumn::make('E-mail', 'email') - ->component('email') - ->attributes(fn ($value, $row, Column $column) => [ - 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', - 'dismissible' => true, - ]), -``` ## Aggregate Columns From a079af2b61eecf356961e2735db624b6324391b3 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:07:38 +0100 Subject: [PATCH 30/66] D --- docs/_index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_index.md b/docs/_index.md index 854891848..0b844a2c9 100644 --- a/docs/_index.md +++ b/docs/_index.md @@ -2,5 +2,5 @@ title: v3 slogan: A dynamic table component for Laravel Livewire. githubUrl: https://github.com/rappasoft/laravel-livewire-tables -branch: master +branch: AddExtraWithsExtraWithCounts --- From 288dee8e3c2c663a2baab8587c60abeb51709f58 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:11:47 +0100 Subject: [PATCH 31/66] Test --- docs/columns/types/_index.md | 4 ++++ docs/columns/types/component_column.md | 29 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 docs/columns/types/_index.md create mode 100644 docs/columns/types/component_column.md diff --git a/docs/columns/types/_index.md b/docs/columns/types/_index.md new file mode 100644 index 000000000..21416dd68 --- /dev/null +++ b/docs/columns/types/_index.md @@ -0,0 +1,4 @@ +--- +title: Types +weight: 5 +--- diff --git a/docs/columns/types/component_column.md b/docs/columns/types/component_column.md new file mode 100644 index 000000000..77d8cb12d --- /dev/null +++ b/docs/columns/types/component_column.md @@ -0,0 +1,29 @@ +--- +title: Component Columns +weight: 4 +--- +## Component Columns + +Component columns let you specify a component name and attributes and provides the column value to the slot. + +```php +// Before +Column::make("Email", "email") + ->format(function ($value) { + return view('components.alert') + ->with('attributes', new ComponentAttributeBag([ + 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', + 'dismissible' => true, + ])) + ->with('slot', $value); + }), + +// After +ComponentColumn::make('E-mail', 'email') + ->component('email') + ->attributes(fn ($value, $row, Column $column) => [ + 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', + 'dismissible' => true, + ]), +``` + From fd4f6144cf8648c152566a2cd98ad7b68134f4c0 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:12:46 +0100 Subject: [PATCH 32/66] Revert Docs Change --- docs/columns/types/_index.md | 4 ---- docs/columns/types/component_column.md | 29 -------------------------- 2 files changed, 33 deletions(-) delete mode 100644 docs/columns/types/_index.md delete mode 100644 docs/columns/types/component_column.md diff --git a/docs/columns/types/_index.md b/docs/columns/types/_index.md deleted file mode 100644 index 21416dd68..000000000 --- a/docs/columns/types/_index.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -title: Types -weight: 5 ---- diff --git a/docs/columns/types/component_column.md b/docs/columns/types/component_column.md deleted file mode 100644 index 77d8cb12d..000000000 --- a/docs/columns/types/component_column.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -title: Component Columns -weight: 4 ---- -## Component Columns - -Component columns let you specify a component name and attributes and provides the column value to the slot. - -```php -// Before -Column::make("Email", "email") - ->format(function ($value) { - return view('components.alert') - ->with('attributes', new ComponentAttributeBag([ - 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', - 'dismissible' => true, - ])) - ->with('slot', $value); - }), - -// After -ComponentColumn::make('E-mail', 'email') - ->component('email') - ->attributes(fn ($value, $row, Column $column) => [ - 'type' => Str::endsWith($value, 'example.org') ? 'success' : 'danger', - 'dismissible' => true, - ]), -``` - From 5f02d70369de1ee1ceb0c0118908eeeab91947c0 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:16:36 +0100 Subject: [PATCH 33/66] Add start of docs for new columns --- docs/column-types/avg_column.md | 5 +++++ docs/column-types/count_column.md | 5 +++++ docs/column-types/sum_column.md | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/docs/column-types/avg_column.md b/docs/column-types/avg_column.md index 54e38e48f..ea5d7d2ed 100644 --- a/docs/column-types/avg_column.md +++ b/docs/column-types/avg_column.md @@ -2,3 +2,8 @@ title: Avg Columns weight: 4 --- + +## Avg Columns + +Avg columns provide an easy way to display the "Average" of a field on a relation. + diff --git a/docs/column-types/count_column.md b/docs/column-types/count_column.md index 79a8fb7dc..86d51deea 100644 --- a/docs/column-types/count_column.md +++ b/docs/column-types/count_column.md @@ -2,3 +2,8 @@ title: Count Columns weight: 4 --- + +## Count Columns + +Count columns provide an easy way to display the "Count" of a relation. + diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md index 05bba3278..ffd3fc62d 100644 --- a/docs/column-types/sum_column.md +++ b/docs/column-types/sum_column.md @@ -2,3 +2,8 @@ title: Sum Columns weight: 4 --- + +## Sum Columns + +Sum columns provide an easy way to display the "Sum" of a field on a relation. + From 49ee96819a43a120635c9fa5a1e8cc99801ef319 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:17:51 +0100 Subject: [PATCH 34/66] Add Array Column doc --- docs/column-types/array_column.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 docs/column-types/array_column.md diff --git a/docs/column-types/array_column.md b/docs/column-types/array_column.md new file mode 100644 index 000000000..659949da4 --- /dev/null +++ b/docs/column-types/array_column.md @@ -0,0 +1,9 @@ +--- +title: Array Columns +weight: 4 +--- + +## Array Columns + +Array columns provide an easy way to work with and display an array of data from a field. + From 9f0c5b06bda5a5d19c35d9f2ce3400d8367b828f Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:18:49 +0100 Subject: [PATCH 35/66] Add Livewire Component Column --- docs/column-types/livewire_component_column.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 docs/column-types/livewire_component_column.md diff --git a/docs/column-types/livewire_component_column.md b/docs/column-types/livewire_component_column.md new file mode 100644 index 000000000..b4ac665ba --- /dev/null +++ b/docs/column-types/livewire_component_column.md @@ -0,0 +1,6 @@ +--- +title: Livewire Component Columns (beta) +weight: 4 +--- + +## Livewire Component Columns \ No newline at end of file From 5629981bd7c3ebe2cae3eb939550d0f60368200c Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:19:26 +0100 Subject: [PATCH 36/66] Add weights --- docs/column-types/array_column.md | 2 +- docs/column-types/avg_column.md | 2 +- docs/column-types/boolean_columns.md | 2 +- docs/column-types/color_columns.md | 2 +- docs/column-types/component_column.md | 2 +- docs/column-types/count_column.md | 2 +- docs/column-types/date_columns.md | 2 +- docs/column-types/image_columns.md | 2 +- docs/column-types/link_columns.md | 2 +- docs/column-types/livewire_component_column.md | 2 +- docs/column-types/sum_column.md | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/column-types/array_column.md b/docs/column-types/array_column.md index 659949da4..5a3e4e33d 100644 --- a/docs/column-types/array_column.md +++ b/docs/column-types/array_column.md @@ -1,6 +1,6 @@ --- title: Array Columns -weight: 4 +weight: 1 --- ## Array Columns diff --git a/docs/column-types/avg_column.md b/docs/column-types/avg_column.md index ea5d7d2ed..fd837287e 100644 --- a/docs/column-types/avg_column.md +++ b/docs/column-types/avg_column.md @@ -1,6 +1,6 @@ --- title: Avg Columns -weight: 4 +weight: 2 --- ## Avg Columns diff --git a/docs/column-types/boolean_columns.md b/docs/column-types/boolean_columns.md index 51efb3bda..b697b72fd 100644 --- a/docs/column-types/boolean_columns.md +++ b/docs/column-types/boolean_columns.md @@ -1,6 +1,6 @@ --- title: Boolean Columns -weight: 4 +weight: 3 --- ## Boolean Columns diff --git a/docs/column-types/color_columns.md b/docs/column-types/color_columns.md index aa3ae8d9a..bc0183339 100644 --- a/docs/column-types/color_columns.md +++ b/docs/column-types/color_columns.md @@ -1,6 +1,6 @@ --- title: Color Columns -weight: 4 +weight: 5 --- ## Color Columns diff --git a/docs/column-types/component_column.md b/docs/column-types/component_column.md index 77d8cb12d..0c31a21a1 100644 --- a/docs/column-types/component_column.md +++ b/docs/column-types/component_column.md @@ -1,6 +1,6 @@ --- title: Component Columns -weight: 4 +weight: 6 --- ## Component Columns diff --git a/docs/column-types/count_column.md b/docs/column-types/count_column.md index 86d51deea..389688c61 100644 --- a/docs/column-types/count_column.md +++ b/docs/column-types/count_column.md @@ -1,6 +1,6 @@ --- title: Count Columns -weight: 4 +weight: 7 --- ## Count Columns diff --git a/docs/column-types/date_columns.md b/docs/column-types/date_columns.md index f177132fb..4a07c4614 100644 --- a/docs/column-types/date_columns.md +++ b/docs/column-types/date_columns.md @@ -1,6 +1,6 @@ --- title: Date Columns -weight: 4 +weight: 8 --- ## Date Columns diff --git a/docs/column-types/image_columns.md b/docs/column-types/image_columns.md index 4c3c407c8..4dce16ea8 100644 --- a/docs/column-types/image_columns.md +++ b/docs/column-types/image_columns.md @@ -1,6 +1,6 @@ --- title: Image Columns -weight: 4 +weight: 9 --- ## Image Columns diff --git a/docs/column-types/link_columns.md b/docs/column-types/link_columns.md index d1484bdb3..e9156c4a2 100644 --- a/docs/column-types/link_columns.md +++ b/docs/column-types/link_columns.md @@ -1,6 +1,6 @@ --- title: Link Columns -weight: 4 +weight: 10 --- ## Link Columns diff --git a/docs/column-types/livewire_component_column.md b/docs/column-types/livewire_component_column.md index b4ac665ba..6fe38e291 100644 --- a/docs/column-types/livewire_component_column.md +++ b/docs/column-types/livewire_component_column.md @@ -1,6 +1,6 @@ --- title: Livewire Component Columns (beta) -weight: 4 +weight: 11 --- ## Livewire Component Columns \ No newline at end of file diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md index ffd3fc62d..ac644e6d1 100644 --- a/docs/column-types/sum_column.md +++ b/docs/column-types/sum_column.md @@ -1,6 +1,6 @@ --- title: Sum Columns -weight: 4 +weight: 12 --- ## Sum Columns From aaf94e599c0319e6880fc9d604b0f7e04d957828 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:22:14 +0100 Subject: [PATCH 37/66] Adjust Col Docs --- docs/column-types/array_column.md | 4 +--- docs/column-types/avg_column.md | 4 +--- docs/column-types/boolean_columns.md | 2 -- docs/column-types/button_group_column.md | 1 - docs/column-types/color_columns.md | 2 -- docs/column-types/component_column.md | 1 - docs/column-types/count_column.md | 4 +--- docs/column-types/date_columns.md | 2 -- docs/column-types/image_columns.md | 1 - docs/column-types/link_columns.md | 1 - docs/column-types/livewire_component_column.md | 3 +-- docs/column-types/sum_column.md | 3 +-- 12 files changed, 5 insertions(+), 23 deletions(-) diff --git a/docs/column-types/array_column.md b/docs/column-types/array_column.md index 5a3e4e33d..4cdc8106a 100644 --- a/docs/column-types/array_column.md +++ b/docs/column-types/array_column.md @@ -1,9 +1,7 @@ --- -title: Array Columns +title: Array Columns (beta) weight: 1 --- -## Array Columns - Array columns provide an easy way to work with and display an array of data from a field. diff --git a/docs/column-types/avg_column.md b/docs/column-types/avg_column.md index fd837287e..74c113781 100644 --- a/docs/column-types/avg_column.md +++ b/docs/column-types/avg_column.md @@ -1,9 +1,7 @@ --- -title: Avg Columns +title: Avg Columns (beta) weight: 2 --- -## Avg Columns - Avg columns provide an easy way to display the "Average" of a field on a relation. diff --git a/docs/column-types/boolean_columns.md b/docs/column-types/boolean_columns.md index b697b72fd..32a759918 100644 --- a/docs/column-types/boolean_columns.md +++ b/docs/column-types/boolean_columns.md @@ -3,8 +3,6 @@ title: Boolean Columns weight: 3 --- -## Boolean Columns - Boolean columns are good if you have a column type that is a true/false, or 0/1 value. For example: diff --git a/docs/column-types/button_group_column.md b/docs/column-types/button_group_column.md index aa314c40d..5a3390049 100644 --- a/docs/column-types/button_group_column.md +++ b/docs/column-types/button_group_column.md @@ -2,7 +2,6 @@ title: Button Group Columns weight: 4 --- -## Button Group Columns Button group columns let you provide an array of LinkColumns to display in a single cell. diff --git a/docs/column-types/color_columns.md b/docs/column-types/color_columns.md index bc0183339..e4920144d 100644 --- a/docs/column-types/color_columns.md +++ b/docs/column-types/color_columns.md @@ -3,8 +3,6 @@ title: Color Columns weight: 5 --- -## Color Columns - Color columns provide an easy way to a Color in a Column You may pass either pass a CSS-compliant colour as a field diff --git a/docs/column-types/component_column.md b/docs/column-types/component_column.md index 0c31a21a1..0db9c9483 100644 --- a/docs/column-types/component_column.md +++ b/docs/column-types/component_column.md @@ -2,7 +2,6 @@ title: Component Columns weight: 6 --- -## Component Columns Component columns let you specify a component name and attributes and provides the column value to the slot. diff --git a/docs/column-types/count_column.md b/docs/column-types/count_column.md index 389688c61..3c7e0de35 100644 --- a/docs/column-types/count_column.md +++ b/docs/column-types/count_column.md @@ -1,9 +1,7 @@ --- -title: Count Columns +title: Count Columns (beta) weight: 7 --- -## Count Columns - Count columns provide an easy way to display the "Count" of a relation. diff --git a/docs/column-types/date_columns.md b/docs/column-types/date_columns.md index 4a07c4614..abc8bbb42 100644 --- a/docs/column-types/date_columns.md +++ b/docs/column-types/date_columns.md @@ -3,8 +3,6 @@ title: Date Columns weight: 8 --- -## Date Columns - Date columns provide an easy way to display dates in a given format, without having to use repetitive format() methods or partial views. You may pass either a DateTime object, in which you can define an "outputFormat" diff --git a/docs/column-types/image_columns.md b/docs/column-types/image_columns.md index 4dce16ea8..f816280af 100644 --- a/docs/column-types/image_columns.md +++ b/docs/column-types/image_columns.md @@ -2,7 +2,6 @@ title: Image Columns weight: 9 --- -## Image Columns Image columns provide a way to display images in your table without having to use `format()` or partial views: diff --git a/docs/column-types/link_columns.md b/docs/column-types/link_columns.md index e9156c4a2..cba4bc887 100644 --- a/docs/column-types/link_columns.md +++ b/docs/column-types/link_columns.md @@ -2,7 +2,6 @@ title: Link Columns weight: 10 --- -## Link Columns Link columns provide a way to display HTML links in your table without having to use `format()` or partial views: diff --git a/docs/column-types/livewire_component_column.md b/docs/column-types/livewire_component_column.md index 6fe38e291..b75172881 100644 --- a/docs/column-types/livewire_component_column.md +++ b/docs/column-types/livewire_component_column.md @@ -1,6 +1,5 @@ --- -title: Livewire Component Columns (beta) +title: Livewire Component (beta) weight: 11 --- -## Livewire Component Columns \ No newline at end of file diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md index ac644e6d1..78167a12e 100644 --- a/docs/column-types/sum_column.md +++ b/docs/column-types/sum_column.md @@ -1,9 +1,8 @@ --- -title: Sum Columns +title: Sum Columns (beta) weight: 12 --- -## Sum Columns Sum columns provide an easy way to display the "Sum" of a field on a relation. From fa9d6778b5b6ed5625dd8fa93b9a2583179aee55 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:24:15 +0100 Subject: [PATCH 38/66] Adding Docs --- docs/column-types/array_column.md | 7 +++++++ docs/column-types/avg_column.md | 5 +++++ docs/column-types/sum_column.md | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/column-types/array_column.md b/docs/column-types/array_column.md index 4cdc8106a..69fd002e7 100644 --- a/docs/column-types/array_column.md +++ b/docs/column-types/array_column.md @@ -5,3 +5,10 @@ weight: 1 Array columns provide an easy way to work with and display an array of data from a field. +``` + ArrayColumn::make('notes', 'name') + ->data(fn($value, $row) => ($row->notes)) + ->outputFormat(fn($index, $value) => "".$value->name."") + ->separator('
') + ->sortable(), +``` \ No newline at end of file diff --git a/docs/column-types/avg_column.md b/docs/column-types/avg_column.md index 74c113781..b3cdfba76 100644 --- a/docs/column-types/avg_column.md +++ b/docs/column-types/avg_column.md @@ -5,3 +5,8 @@ weight: 2 Avg columns provide an easy way to display the "Average" of a field on a relation. +``` + AvgColumn::make('Average Related User Age') + ->setDataSource('users','age') + ->sortable(), +``` \ No newline at end of file diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md index 78167a12e..29cc52290 100644 --- a/docs/column-types/sum_column.md +++ b/docs/column-types/sum_column.md @@ -5,4 +5,8 @@ weight: 12 Sum columns provide an easy way to display the "Sum" of a field on a relation. - +``` + SumColumn::make('Total Age of Related Users') + ->setDataSource('users','age') + ->sortable(), +``` From 62f6d6275fc8a6406494d3089bac283fec6d2665 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 6 Jul 2024 00:26:03 +0100 Subject: [PATCH 39/66] Add more docs for Agg cols --- docs/column-types/avg_column.md | 8 +++++--- docs/column-types/count_column.md | 7 +++++++ docs/column-types/sum_column.md | 8 +++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/column-types/avg_column.md b/docs/column-types/avg_column.md index b3cdfba76..faa653ca2 100644 --- a/docs/column-types/avg_column.md +++ b/docs/column-types/avg_column.md @@ -7,6 +7,8 @@ Avg columns provide an easy way to display the "Average" of a field on a relatio ``` AvgColumn::make('Average Related User Age') - ->setDataSource('users','age') - ->sortable(), -``` \ No newline at end of file + ->setDataSource('users','age') + ->sortable(), +``` + +The "sortable()" callback can accept a callback, or you can use the default behaviour, which calculates the correct field to sort on. \ No newline at end of file diff --git a/docs/column-types/count_column.md b/docs/column-types/count_column.md index 3c7e0de35..d33cf5df9 100644 --- a/docs/column-types/count_column.md +++ b/docs/column-types/count_column.md @@ -5,3 +5,10 @@ weight: 7 Count columns provide an easy way to display the "Count" of a relation. +``` + CountColumn::make('Related Users') + ->setDataSource('users') + ->sortable(), +``` + +The "sortable()" callback can accept a callback, or you can use the default behaviour, which calculates the correct field to sort on. \ No newline at end of file diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md index 29cc52290..7f99f7cab 100644 --- a/docs/column-types/sum_column.md +++ b/docs/column-types/sum_column.md @@ -3,10 +3,12 @@ title: Sum Columns (beta) weight: 12 --- - Sum columns provide an easy way to display the "Sum" of a field on a relation. + ``` SumColumn::make('Total Age of Related Users') - ->setDataSource('users','age') - ->sortable(), + ->setDataSource('users','age') + ->sortable(), ``` + +The "sortable()" callback can accept a callback, or you can use the default behaviour, which calculates the correct field to sort on. \ No newline at end of file From 7c445dc1ea04b31a5dd70fdec433506df3501001 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 21:38:29 +0100 Subject: [PATCH 40/66] Add exceptions for empty data source, add standard tests --- src/Views/Columns/AggregateColumn.php | 17 ++++ src/Views/Columns/AvgColumn.php | 17 ++++ src/Views/Columns/CountColumn.php | 18 ++++ src/Views/Columns/SumColumn.php | 16 ++++ tests/Attributes/AggregateColumnProvider.php | 13 +++ tests/Views/Columns/AvgColumnTest.php | 86 ++++++++++++++++++++ tests/Views/Columns/CountColumnTest.php | 38 +++++++++ tests/Views/Columns/SumColumnTest.php | 86 ++++++++++++++++++++ 8 files changed, 291 insertions(+) create mode 100644 tests/Attributes/AggregateColumnProvider.php create mode 100644 tests/Views/Columns/AvgColumnTest.php create mode 100644 tests/Views/Columns/CountColumnTest.php create mode 100644 tests/Views/Columns/SumColumnTest.php diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 295027a7c..1556d96fe 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -2,10 +2,13 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class AggregateColumn extends Column { @@ -26,4 +29,18 @@ public function __construct(string $title, ?string $from = null) $this->label(fn () => null); } } + + public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + { + if (!isset($this->dataSource)) + { + throw new DataTableConfigurationException('You must specify a data source'); + } + else + { + return parent::getContents($row); + } + + } + } diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index 26c69954a..4384a5fa8 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -2,10 +2,13 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class AvgColumn extends AggregateColumn { @@ -22,4 +25,18 @@ public function __construct(string $title, ?string $from = null) $this->label(fn () => null); } } + + public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + { + if (!isset($this->dataSource)) + { + throw new DataTableConfigurationException('You must specify a data source'); + } + else + { + return parent::getContents($row); + } + + } + } diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 65b3f1a30..86a86fd2a 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -2,10 +2,13 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class CountColumn extends AggregateColumn { @@ -22,4 +25,19 @@ public function __construct(string $title, ?string $from = null) $this->label(fn () => null); } } + + public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + { + if (!isset($this->dataSource)) + { + throw new DataTableConfigurationException('You must specify a data source'); + } + else + { + return parent::getContents($row); + } + + } + + } diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index ef3bc9d73..2572d7f8c 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -2,10 +2,13 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class SumColumn extends AggregateColumn { @@ -19,4 +22,17 @@ public function __construct(string $title, ?string $from = null) { parent::__construct($title, $from); } + + public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + { + if (!isset($this->dataSource)) + { + throw new DataTableConfigurationException('You must specify a data source'); + } + else + { + return parent::getContents($row); + } + + } } diff --git a/tests/Attributes/AggregateColumnProvider.php b/tests/Attributes/AggregateColumnProvider.php new file mode 100644 index 000000000..7234ddc53 --- /dev/null +++ b/tests/Attributes/AggregateColumnProvider.php @@ -0,0 +1,13 @@ +assertSame('Average Age', $column->getTitle()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_setup_column_correctly(string $relation_name, string $foreign_field): void + { + $column = AvgColumn::make('Average Age') + ->setDataSource($relation_name,$foreign_field) + ->sortable(); + + $this->assertNotEmpty($column); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_not_skip_set_data_source(string $relation_name, string $foreign_field): void + { + $this->expectException(DataTableConfigurationException::class); + + $column = AvgColumn::make('Average Age') + ->sortable(); + $contents = $column->getContents(Pet::find(1)); + $this->assertNull($contents); + + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_data_source(string $relation_name, string $foreign_field): void + { + $column = AvgColumn::make('Average Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasDataSource()); + $this->assertSame($relation_name, $column->getDataSource()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_foreign_column(string $relation_name, string $foreign_field): void + { + $column = AvgColumn::make('Average Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame($foreign_field, $column->getForeignColumn()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_data_source_fields(string $relation_name, string $foreign_field): void + { + $column = AvgColumn::make('Average Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasDataSource()); + $this->assertSame($relation_name, $column->getDataSource()); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame($foreign_field, $column->getForeignColumn()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_aggregate_method(string $relation_name, string $foreign_field): void + { + $column = AvgColumn::make('Average Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertSame('avg',$column->getAggregateMethod()); + $column->setAggregateMethod('test_avg'); + $this->assertSame('test_avg',$column->getAggregateMethod()); + } + +} diff --git a/tests/Views/Columns/CountColumnTest.php b/tests/Views/Columns/CountColumnTest.php new file mode 100644 index 000000000..a4189a458 --- /dev/null +++ b/tests/Views/Columns/CountColumnTest.php @@ -0,0 +1,38 @@ +assertSame('Total Users', $column->getTitle()); + } + + public function test_can_setup_column_correctly(): void + { + $column = CountColumn::make('Total Users') + ->setDataSource('users') + ->sortable(); + + $this->assertNotEmpty($column); + } + + public function test_can_not_skip_set_data_source(): void + { + $this->expectException(DataTableConfigurationException::class); + + $column = CountColumn::make('Total Users') + ->sortable(); + $contents = $column->getContents(Pet::find(1)); + $this->assertNull($contents); + + } +} diff --git a/tests/Views/Columns/SumColumnTest.php b/tests/Views/Columns/SumColumnTest.php new file mode 100644 index 000000000..f61bc6fbf --- /dev/null +++ b/tests/Views/Columns/SumColumnTest.php @@ -0,0 +1,86 @@ +assertSame('Sum User Age', $column->getTitle()); + } + + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_setup_column_correctly(string $relation_name, string $foreign_field): void + { + $column = SumColumn::make('Sum User Age') + ->setDataSource($relation_name,$foreign_field) + ->sortable(); + + $this->assertNotEmpty($column); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_not_skip_set_data_source(string $relation_name, string $foreign_field): void + { + $this->expectException(DataTableConfigurationException::class); + + $column = SumColumn::make('Sum User Age') + ->sortable(); + $contents = $column->getContents(Pet::find(1)); + $this->assertNull($contents); + + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_data_source(string $relation_name, string $foreign_field): void + { + $column = SumColumn::make('Sum User Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasDataSource()); + $this->assertSame($relation_name, $column->getDataSource()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_foreign_column(string $relation_name, string $foreign_field): void + { + $column = SumColumn::make('Sum User Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame($foreign_field, $column->getForeignColumn()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_data_source_fields(string $relation_name, string $foreign_field): void + { + $column = SumColumn::make('Sum User Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasDataSource()); + $this->assertSame($relation_name, $column->getDataSource()); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame($foreign_field, $column->getForeignColumn()); + } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_get_aggregate_method(string $relation_name, string $foreign_field): void + { + $column = SumColumn::make('Sum User Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertSame('sum',$column->getAggregateMethod()); + $column->setAggregateMethod('test_sum'); + $this->assertSame('test_sum',$column->getAggregateMethod()); + } +} From fb85218f153a898b381db5386a20d0d2e5cdb396 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 20:38:57 +0000 Subject: [PATCH 41/66] Fix styling --- src/Views/Columns/AggregateColumn.php | 12 +++----- src/Views/Columns/AvgColumn.php | 12 +++----- src/Views/Columns/CountColumn.php | 13 +++----- src/Views/Columns/SumColumn.php | 11 +++---- tests/Attributes/AggregateColumnProvider.php | 4 +-- tests/Views/Columns/AvgColumnTest.php | 31 ++++++++++---------- tests/Views/Columns/CountColumnTest.php | 6 ++-- tests/Views/Columns/SumColumnTest.php | 31 ++++++++++---------- 8 files changed, 51 insertions(+), 69 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 1556d96fe..13cdb04d7 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -4,11 +4,11 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\HtmlString; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class AggregateColumn extends Column { @@ -32,15 +32,11 @@ public function __construct(string $title, ?string $from = null) public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { - if (!isset($this->dataSource)) - { + if (! isset($this->dataSource)) { throw new DataTableConfigurationException('You must specify a data source'); - } - else - { + } else { return parent::getContents($row); } - - } + } } diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index 4384a5fa8..bd1d3b4a5 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -4,11 +4,11 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\HtmlString; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class AvgColumn extends AggregateColumn { @@ -28,15 +28,11 @@ public function __construct(string $title, ?string $from = null) public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { - if (!isset($this->dataSource)) - { + if (! isset($this->dataSource)) { throw new DataTableConfigurationException('You must specify a data source'); - } - else - { + } else { return parent::getContents($row); } - - } + } } diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 86a86fd2a..4371b50a3 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -4,11 +4,11 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\HtmlString; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class CountColumn extends AggregateColumn { @@ -28,16 +28,11 @@ public function __construct(string $title, ?string $from = null) public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { - if (!isset($this->dataSource)) - { + if (! isset($this->dataSource)) { throw new DataTableConfigurationException('You must specify a data source'); - } - else - { + } else { return parent::getContents($row); } - - } - + } } diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 2572d7f8c..0516b238a 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -4,11 +4,11 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\HtmlString; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; class SumColumn extends AggregateColumn { @@ -25,14 +25,11 @@ public function __construct(string $title, ?string $from = null) public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { - if (!isset($this->dataSource)) - { + if (! isset($this->dataSource)) { throw new DataTableConfigurationException('You must specify a data source'); - } - else - { + } else { return parent::getContents($row); } - + } } diff --git a/tests/Attributes/AggregateColumnProvider.php b/tests/Attributes/AggregateColumnProvider.php index 7234ddc53..8e8c3f529 100644 --- a/tests/Attributes/AggregateColumnProvider.php +++ b/tests/Attributes/AggregateColumnProvider.php @@ -1,4 +1,4 @@ -setDataSource($relation_name,$foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertNotEmpty($column); } @@ -34,7 +34,7 @@ public function test_can_not_skip_set_data_source(string $relation_name, string $this->expectException(DataTableConfigurationException::class); $column = AvgColumn::make('Average Age') - ->sortable(); + ->sortable(); $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); @@ -44,8 +44,8 @@ public function test_can_not_skip_set_data_source(string $relation_name, string public function test_can_get_data_source(string $relation_name, string $foreign_field): void { $column = AvgColumn::make('Average Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertTrue($column->hasDataSource()); $this->assertSame($relation_name, $column->getDataSource()); } @@ -54,8 +54,8 @@ public function test_can_get_data_source(string $relation_name, string $foreign_ public function test_can_get_foreign_column(string $relation_name, string $foreign_field): void { $column = AvgColumn::make('Average Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertTrue($column->hasForeignColumn()); $this->assertSame($foreign_field, $column->getForeignColumn()); } @@ -64,8 +64,8 @@ public function test_can_get_foreign_column(string $relation_name, string $forei public function test_can_get_data_source_fields(string $relation_name, string $foreign_field): void { $column = AvgColumn::make('Average Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertTrue($column->hasDataSource()); $this->assertSame($relation_name, $column->getDataSource()); $this->assertTrue($column->hasForeignColumn()); @@ -76,11 +76,10 @@ public function test_can_get_data_source_fields(string $relation_name, string $f public function test_can_get_aggregate_method(string $relation_name, string $foreign_field): void { $column = AvgColumn::make('Average Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); - $this->assertSame('avg',$column->getAggregateMethod()); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertSame('avg', $column->getAggregateMethod()); $column->setAggregateMethod('test_avg'); - $this->assertSame('test_avg',$column->getAggregateMethod()); + $this->assertSame('test_avg', $column->getAggregateMethod()); } - } diff --git a/tests/Views/Columns/CountColumnTest.php b/tests/Views/Columns/CountColumnTest.php index a4189a458..25beba443 100644 --- a/tests/Views/Columns/CountColumnTest.php +++ b/tests/Views/Columns/CountColumnTest.php @@ -19,8 +19,8 @@ public function test_can_set_the_column_title(): void public function test_can_setup_column_correctly(): void { $column = CountColumn::make('Total Users') - ->setDataSource('users') - ->sortable(); + ->setDataSource('users') + ->sortable(); $this->assertNotEmpty($column); } @@ -30,7 +30,7 @@ public function test_can_not_skip_set_data_source(): void $this->expectException(DataTableConfigurationException::class); $column = CountColumn::make('Total Users') - ->sortable(); + ->sortable(); $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); diff --git a/tests/Views/Columns/SumColumnTest.php b/tests/Views/Columns/SumColumnTest.php index f61bc6fbf..74af01db5 100644 --- a/tests/Views/Columns/SumColumnTest.php +++ b/tests/Views/Columns/SumColumnTest.php @@ -2,12 +2,12 @@ namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; +use Rappasoft\LaravelLivewireTables\Tests\Attributes\AggregateColumnProvider; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Tests\TestCase; use Rappasoft\LaravelLivewireTables\Views\Columns\SumColumn; -use Rappasoft\LaravelLivewireTables\Tests\Attributes\AggregateColumnProvider; -use PHPUnit\Framework\Attributes\DataProviderExternal; final class SumColumnTest extends TestCase { @@ -17,14 +17,13 @@ public function test_can_set_the_column_title(): void $this->assertSame('Sum User Age', $column->getTitle()); } - #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] public function test_can_setup_column_correctly(string $relation_name, string $foreign_field): void { $column = SumColumn::make('Sum User Age') - ->setDataSource($relation_name,$foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertNotEmpty($column); } @@ -35,7 +34,7 @@ public function test_can_not_skip_set_data_source(string $relation_name, string $this->expectException(DataTableConfigurationException::class); $column = SumColumn::make('Sum User Age') - ->sortable(); + ->sortable(); $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); @@ -45,8 +44,8 @@ public function test_can_not_skip_set_data_source(string $relation_name, string public function test_can_get_data_source(string $relation_name, string $foreign_field): void { $column = SumColumn::make('Sum User Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertTrue($column->hasDataSource()); $this->assertSame($relation_name, $column->getDataSource()); } @@ -55,8 +54,8 @@ public function test_can_get_data_source(string $relation_name, string $foreign_ public function test_can_get_foreign_column(string $relation_name, string $foreign_field): void { $column = SumColumn::make('Sum User Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertTrue($column->hasForeignColumn()); $this->assertSame($foreign_field, $column->getForeignColumn()); } @@ -65,8 +64,8 @@ public function test_can_get_foreign_column(string $relation_name, string $forei public function test_can_get_data_source_fields(string $relation_name, string $foreign_field): void { $column = SumColumn::make('Sum User Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); $this->assertTrue($column->hasDataSource()); $this->assertSame($relation_name, $column->getDataSource()); $this->assertTrue($column->hasForeignColumn()); @@ -77,10 +76,10 @@ public function test_can_get_data_source_fields(string $relation_name, string $f public function test_can_get_aggregate_method(string $relation_name, string $foreign_field): void { $column = SumColumn::make('Sum User Age') - ->setDataSource($relation_name, $foreign_field) - ->sortable(); - $this->assertSame('sum',$column->getAggregateMethod()); + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertSame('sum', $column->getAggregateMethod()); $column->setAggregateMethod('test_sum'); - $this->assertSame('test_sum',$column->getAggregateMethod()); + $this->assertSame('test_sum', $column->getAggregateMethod()); } } From dbdb365a3ce460655f5a5b8697baec1f7602246c Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:24:40 +0100 Subject: [PATCH 42/66] Add additional tests --- docs/_index.md | 2 +- src/Views/Columns/AggregateColumn.php | 6 +-- src/Views/Columns/AvgColumn.php | 6 +-- src/Views/Columns/CountColumn.php | 6 +-- src/Views/Columns/SumColumn.php | 3 +- .../AggregateColumnConfiguration.php | 2 +- .../ComponentConfigurationTest.php | 46 +++++++++++++++++++ 7 files changed, 56 insertions(+), 15 deletions(-) diff --git a/docs/_index.md b/docs/_index.md index 0b844a2c9..854891848 100644 --- a/docs/_index.md +++ b/docs/_index.md @@ -2,5 +2,5 @@ title: v3 slogan: A dynamic table component for Laravel Livewire. githubUrl: https://github.com/rappasoft/laravel-livewire-tables -branch: AddExtraWithsExtraWithCounts +branch: master --- diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 13cdb04d7..38d17574c 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -24,10 +24,8 @@ class AggregateColumn extends Column public function __construct(string $title, ?string $from = null) { - parent::__construct($title, $from); - if (! isset($from)) { - $this->label(fn () => null); - } + parent::__construct($title, null); + $this->label(fn () => null); } public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index bd1d3b4a5..dfc216446 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -20,10 +20,8 @@ class AvgColumn extends AggregateColumn public function __construct(string $title, ?string $from = null) { - parent::__construct($title, $from); - if (! isset($from)) { - $this->label(fn () => null); - } + parent::__construct($title, null); + $this->label(fn () => null); } public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 4371b50a3..27cd6393d 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -20,10 +20,8 @@ class CountColumn extends AggregateColumn public function __construct(string $title, ?string $from = null) { - parent::__construct($title, $from); - if (! isset($from)) { - $this->label(fn () => null); - } + parent::__construct($title, null); + $this->label(fn () => null); } public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 0516b238a..61e5d3055 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -20,7 +20,8 @@ class SumColumn extends AggregateColumn public function __construct(string $title, ?string $from = null) { - parent::__construct($title, $from); + parent::__construct($title, null); + $this->label(fn () => null); } public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php index 28b93f940..b12c5a3b3 100644 --- a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -36,7 +36,7 @@ public function setForeignColumn(string $foreignColumn): self return $this; } - public function setDefaultLabel() + public function setDefaultLabel(): void { $this->label(function ($row, Column $column) { if ($this->hasForeignColumn()) { diff --git a/tests/Traits/Configuration/ComponentConfigurationTest.php b/tests/Traits/Configuration/ComponentConfigurationTest.php index 001cea2a0..43aa5e847 100644 --- a/tests/Traits/Configuration/ComponentConfigurationTest.php +++ b/tests/Traits/Configuration/ComponentConfigurationTest.php @@ -300,4 +300,50 @@ public function test_can_set_hide_configurable_areas_when_reordering_status(): v $this->basicTable->setHideConfigurableAreasWhenReorderingStatus(true); } + + public function test_no_extra_withs_by_default(): void + { + $this->assertFalse($this->basicTable->hasExtraWiths()); + $this->assertEmpty($this->basicTable->getExtraWiths()); + } + + public function test_can_add_extra_with(): void + { + $this->assertFalse($this->basicTable->hasExtraWiths()); + $this->assertEmpty($this->basicTable->getExtraWiths()); + $this->basicTable->addExtraWith('user'); + $this->assertTrue($this->basicTable->hasExtraWiths()); + $this->assertSame(['user'], $this->basicTable->getExtraWiths()); + } + + public function test_no_extra_with_counts_by_default(): void + { + $this->assertFalse($this->basicTable->hasExtraWithCounts()); + $this->assertEmpty($this->basicTable->getExtraWithCounts()); + } + + public function test_can_add_extra_with_count(): void + { + $this->assertFalse($this->basicTable->hasExtraWithCounts()); + $this->assertEmpty($this->basicTable->getExtraWithCounts()); + $this->basicTable->addExtraWithCount('users'); + $this->assertTrue($this->basicTable->hasExtraWithCounts()); + $this->assertSame(['users'], $this->basicTable->getExtraWithCounts()); + } + + public function test_no_extra_with_sums_by_default(): void + { + $this->assertFalse($this->basicTable->hasExtraWithSums()); + $this->assertEmpty($this->basicTable->getExtraWithSums()); + } + + public function test_can_add_extra_with_sum(): void + { + $this->assertFalse($this->basicTable->hasExtraWithSums()); + $this->assertEmpty($this->basicTable->getExtraWithSums()); + $this->basicTable->addExtraWithSum('users','age'); + $this->assertTrue($this->basicTable->hasExtraWithSums()); + $this->assertSame([['table' => 'users', 'field' => 'age']], $this->basicTable->getExtraWithSums()); + } + } From 10044b0b4635c39128ff7dd069dd70dcf9a91621 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 21:25:06 +0000 Subject: [PATCH 43/66] Fix styling --- tests/Traits/Configuration/ComponentConfigurationTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Traits/Configuration/ComponentConfigurationTest.php b/tests/Traits/Configuration/ComponentConfigurationTest.php index 43aa5e847..e096eca3c 100644 --- a/tests/Traits/Configuration/ComponentConfigurationTest.php +++ b/tests/Traits/Configuration/ComponentConfigurationTest.php @@ -341,9 +341,8 @@ public function test_can_add_extra_with_sum(): void { $this->assertFalse($this->basicTable->hasExtraWithSums()); $this->assertEmpty($this->basicTable->getExtraWithSums()); - $this->basicTable->addExtraWithSum('users','age'); + $this->basicTable->addExtraWithSum('users', 'age'); $this->assertTrue($this->basicTable->hasExtraWithSums()); $this->assertSame([['table' => 'users', 'field' => 'age']], $this->basicTable->getExtraWithSums()); } - } From 8dc312adc515a52a0b4110604fa2593a8d985c71 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:26:36 +0100 Subject: [PATCH 44/66] Ensure pcov runs on push to master/development/develop --- .github/workflows/run-tests-pcov-pull.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests-pcov-pull.yml b/.github/workflows/run-tests-pcov-pull.yml index 3a24a91ba..f4b0a2749 100644 --- a/.github/workflows/run-tests-pcov-pull.yml +++ b/.github/workflows/run-tests-pcov-pull.yml @@ -1,6 +1,11 @@ name: run-tests-pcov-pull on: + push: + branches: + - 'develop' + - 'development' + - 'master' pull_request: branches: - 'develop' @@ -18,7 +23,7 @@ jobs: laravel: [10] stability: [prefer-dist] - name: PCOV-PULL - ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} + name: PCOV - ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} env: extensionKey: phpextensions-${{ matrix.os }}-P${{ matrix.php }}-withpcov extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pcov,pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo From d5cb666bffdaf36d2f944f1bb036ea9aeca4873d Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:41:34 +0100 Subject: [PATCH 45/66] Add extra tests --- .../Configuration/ComponentConfiguration.php | 4 +- src/Views/Columns/AggregateColumn.php | 2 +- src/Views/Columns/AvgColumn.php | 2 +- src/Views/Columns/CountColumn.php | 2 +- src/Views/Columns/SumColumn.php | 2 +- .../ComponentConfigurationTest.php | 43 +++++++++++++++++++ 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index da4ea2a24..044f5655e 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -113,7 +113,7 @@ public function addExtraWith(string $extraWith): self public function addExtraWiths(array $extraWiths): self { - $this->extraWiths[] = [...$this->extraWiths, ...$extraWiths]; + $this->extraWiths = [...$this->extraWiths, ...$extraWiths]; return $this; } @@ -134,7 +134,7 @@ public function addExtraWithCount(string $extraWithCount): self public function addExtraWithCounts(array $extraWithCounts): self { - $this->extraWithCounts[] = [...$this->extraWithCounts, ...$extraWithCounts]; + $this->extraWithCounts = [...$this->extraWithCounts, ...$extraWithCounts]; return $this; } diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 38d17574c..3ce175e97 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -24,7 +24,7 @@ class AggregateColumn extends Column public function __construct(string $title, ?string $from = null) { - parent::__construct($title, null); + parent::__construct($title, $from); $this->label(fn () => null); } diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index dfc216446..07c72c814 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -20,7 +20,7 @@ class AvgColumn extends AggregateColumn public function __construct(string $title, ?string $from = null) { - parent::__construct($title, null); + parent::__construct($title, $from); $this->label(fn () => null); } diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 27cd6393d..21f1777eb 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -20,7 +20,7 @@ class CountColumn extends AggregateColumn public function __construct(string $title, ?string $from = null) { - parent::__construct($title, null); + parent::__construct($title, $from); $this->label(fn () => null); } diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 61e5d3055..8bfefd52c 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -20,7 +20,7 @@ class SumColumn extends AggregateColumn public function __construct(string $title, ?string $from = null) { - parent::__construct($title, null); + parent::__construct($title, $from); $this->label(fn () => null); } diff --git a/tests/Traits/Configuration/ComponentConfigurationTest.php b/tests/Traits/Configuration/ComponentConfigurationTest.php index e096eca3c..636bcbcb8 100644 --- a/tests/Traits/Configuration/ComponentConfigurationTest.php +++ b/tests/Traits/Configuration/ComponentConfigurationTest.php @@ -316,6 +316,27 @@ public function test_can_add_extra_with(): void $this->assertSame(['user'], $this->basicTable->getExtraWiths()); } + public function test_can_add_extra_withs(): void + { + $this->assertFalse($this->basicTable->hasExtraWiths()); + $this->assertEmpty($this->basicTable->getExtraWiths()); + $this->basicTable->addExtraWiths(['user','pets']); + $this->assertTrue($this->basicTable->hasExtraWiths()); + $this->assertSame(['user','pets'], $this->basicTable->getExtraWiths()); + } + + public function test_can_set_extra_withs(): void + { + $this->assertFalse($this->basicTable->hasExtraWiths()); + $this->assertEmpty($this->basicTable->getExtraWiths()); + $this->basicTable->addExtraWith('test'); + $this->assertSame(['test'], $this->basicTable->getExtraWiths()); + $this->assertTrue($this->basicTable->hasExtraWiths()); + $this->basicTable->setExtraWiths(['user','pets']); + $this->assertTrue($this->basicTable->hasExtraWiths()); + $this->assertSame(['user','pets'], $this->basicTable->getExtraWiths()); + } + public function test_no_extra_with_counts_by_default(): void { $this->assertFalse($this->basicTable->hasExtraWithCounts()); @@ -331,6 +352,27 @@ public function test_can_add_extra_with_count(): void $this->assertSame(['users'], $this->basicTable->getExtraWithCounts()); } + public function test_can_add_extra_with_counts(): void + { + $this->assertFalse($this->basicTable->hasExtraWithCounts()); + $this->assertEmpty($this->basicTable->getExtraWithCounts()); + $this->basicTable->addExtraWithCounts(['user','pets']); + $this->assertTrue($this->basicTable->hasExtraWithCounts()); + $this->assertSame(['user','pets'], $this->basicTable->getExtraWithCounts()); + } + + public function test_can_set_extra_with_counts(): void + { + $this->assertFalse($this->basicTable->hasExtraWithCounts()); + $this->assertEmpty($this->basicTable->getExtraWithCounts()); + $this->basicTable->addExtraWithCount('test'); + $this->assertSame(['test'], $this->basicTable->getExtraWithCounts()); + $this->assertTrue($this->basicTable->hasExtraWithCounts()); + $this->basicTable->setExtraWithCounts(['user','pets']); + $this->assertTrue($this->basicTable->hasExtraWithCounts()); + $this->assertSame(['user','pets'], $this->basicTable->getExtraWithCounts()); + } + public function test_no_extra_with_sums_by_default(): void { $this->assertFalse($this->basicTable->hasExtraWithSums()); @@ -345,4 +387,5 @@ public function test_can_add_extra_with_sum(): void $this->assertTrue($this->basicTable->hasExtraWithSums()); $this->assertSame([['table' => 'users', 'field' => 'age']], $this->basicTable->getExtraWithSums()); } + } From ef62fb18a42240e720ef4de1c5b062aa0a729b8a Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 21:42:01 +0000 Subject: [PATCH 46/66] Fix styling --- .../ComponentConfigurationTest.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/Traits/Configuration/ComponentConfigurationTest.php b/tests/Traits/Configuration/ComponentConfigurationTest.php index 636bcbcb8..7000b505e 100644 --- a/tests/Traits/Configuration/ComponentConfigurationTest.php +++ b/tests/Traits/Configuration/ComponentConfigurationTest.php @@ -320,9 +320,9 @@ public function test_can_add_extra_withs(): void { $this->assertFalse($this->basicTable->hasExtraWiths()); $this->assertEmpty($this->basicTable->getExtraWiths()); - $this->basicTable->addExtraWiths(['user','pets']); + $this->basicTable->addExtraWiths(['user', 'pets']); $this->assertTrue($this->basicTable->hasExtraWiths()); - $this->assertSame(['user','pets'], $this->basicTable->getExtraWiths()); + $this->assertSame(['user', 'pets'], $this->basicTable->getExtraWiths()); } public function test_can_set_extra_withs(): void @@ -332,9 +332,9 @@ public function test_can_set_extra_withs(): void $this->basicTable->addExtraWith('test'); $this->assertSame(['test'], $this->basicTable->getExtraWiths()); $this->assertTrue($this->basicTable->hasExtraWiths()); - $this->basicTable->setExtraWiths(['user','pets']); + $this->basicTable->setExtraWiths(['user', 'pets']); $this->assertTrue($this->basicTable->hasExtraWiths()); - $this->assertSame(['user','pets'], $this->basicTable->getExtraWiths()); + $this->assertSame(['user', 'pets'], $this->basicTable->getExtraWiths()); } public function test_no_extra_with_counts_by_default(): void @@ -356,9 +356,9 @@ public function test_can_add_extra_with_counts(): void { $this->assertFalse($this->basicTable->hasExtraWithCounts()); $this->assertEmpty($this->basicTable->getExtraWithCounts()); - $this->basicTable->addExtraWithCounts(['user','pets']); + $this->basicTable->addExtraWithCounts(['user', 'pets']); $this->assertTrue($this->basicTable->hasExtraWithCounts()); - $this->assertSame(['user','pets'], $this->basicTable->getExtraWithCounts()); + $this->assertSame(['user', 'pets'], $this->basicTable->getExtraWithCounts()); } public function test_can_set_extra_with_counts(): void @@ -368,9 +368,9 @@ public function test_can_set_extra_with_counts(): void $this->basicTable->addExtraWithCount('test'); $this->assertSame(['test'], $this->basicTable->getExtraWithCounts()); $this->assertTrue($this->basicTable->hasExtraWithCounts()); - $this->basicTable->setExtraWithCounts(['user','pets']); + $this->basicTable->setExtraWithCounts(['user', 'pets']); $this->assertTrue($this->basicTable->hasExtraWithCounts()); - $this->assertSame(['user','pets'], $this->basicTable->getExtraWithCounts()); + $this->assertSame(['user', 'pets'], $this->basicTable->getExtraWithCounts()); } public function test_no_extra_with_sums_by_default(): void @@ -387,5 +387,4 @@ public function test_can_add_extra_with_sum(): void $this->assertTrue($this->basicTable->hasExtraWithSums()); $this->assertSame([['table' => 'users', 'field' => 'age']], $this->basicTable->getExtraWithSums()); } - } From 7e7fad5f11e4ad60a32aba5724eaee805b05c32e Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:44:27 +0100 Subject: [PATCH 47/66] Update to use codecov v4 --- .github/workflows/run-tests-pcov-pull.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests-pcov-pull.yml b/.github/workflows/run-tests-pcov-pull.yml index f4b0a2749..ef73e4b42 100644 --- a/.github/workflows/run-tests-pcov-pull.yml +++ b/.github/workflows/run-tests-pcov-pull.yml @@ -91,7 +91,7 @@ jobs: run: php ./vendor/bin/paratest --cache-directory=".phpunit.cache/code-coverage" --strict-coverage --coverage-clover ./coverage.xml --processes=4 - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} with: From 5a2ce7433949f813c1e184cb16ae5c16c30f166e Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:57:05 +0100 Subject: [PATCH 48/66] Add Count Render Test --- tests/Http/Livewire/SpeciesTable.php | 33 +++++++++++++++++++++++++ tests/TestCase.php | 21 ++++++++++++++-- tests/Views/Columns/CountColumnTest.php | 14 ++++++++++- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/Http/Livewire/SpeciesTable.php diff --git a/tests/Http/Livewire/SpeciesTable.php b/tests/Http/Livewire/SpeciesTable.php new file mode 100644 index 000000000..d9c1106b9 --- /dev/null +++ b/tests/Http/Livewire/SpeciesTable.php @@ -0,0 +1,33 @@ +setPrimaryKey('id'); + } + + public function columns(): array + { + return [ + Column::make('ID', 'id') + ->sortable() + ->setSortingPillTitle('Key') + ->setSortingPillDirections('0-9', '9-0'), + Column::make('Name') + ->sortable() + ->searchable(), + CountColumn::make('Pets') + ->setDataSource('pets'), + ]; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index fdcbd44a6..6004adf89 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,7 +9,7 @@ use Livewire\LivewireServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider; -use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{PetsTable,PetsTableUnpaginated}; +use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{PetsTable,PetsTableUnpaginated,SpeciesTable}; use Rappasoft\LaravelLivewireTables\Tests\Models\Breed; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Tests\Models\Species; @@ -18,6 +18,7 @@ class TestCase extends Orchestra { public PetsTable $basicTable; + public SpeciesTable $speciesTable; public PetsTableUnpaginated $unpaginatedTable; @@ -76,7 +77,7 @@ protected function setUp(): void } $this->setupBasicTable(); $this->setupUnpaginatedTable(); - + $this->setupSpeciesTable(); } protected function setupBasicTable() @@ -95,6 +96,22 @@ protected function setupBasicTable() $this->basicTable->render(); } + protected function setupSpeciesTable() + { + $view = view('livewire-tables::datatable'); + $this->speciesTable = new SpeciesTable(); + $this->speciesTable->boot(); + $this->speciesTable->bootedComponentUtilities(); + $this->speciesTable->bootedWithData(); + $this->speciesTable->bootedWithColumns(); + $this->speciesTable->bootedWithColumnSelect(); + $this->speciesTable->bootedWithSecondaryHeader(); + $this->speciesTable->booted(); + $this->speciesTable->renderingWithData($view, []); + $this->speciesTable->renderingWithPagination($view, []); + $this->speciesTable->render(); + } + protected function setupUnpaginatedTable() { diff --git a/tests/Views/Columns/CountColumnTest.php b/tests/Views/Columns/CountColumnTest.php index 25beba443..42c42b641 100644 --- a/tests/Views/Columns/CountColumnTest.php +++ b/tests/Views/Columns/CountColumnTest.php @@ -29,10 +29,22 @@ public function test_can_not_skip_set_data_source(): void { $this->expectException(DataTableConfigurationException::class); - $column = CountColumn::make('Total Users') + $column = CountColumn::make('Average Age') ->sortable(); $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); } + + public function test_renders_correctly(): void + { + $rows = $this->speciesTable->getRows(); + $row1 = $rows->first(); + $column = CountColumn::make('Pets') + ->setDataSource('pets'); + $contents = $column->getContents($rows->first()); + $this->assertSame('2',$contents); + $contents = $column->getContents($rows->last()); + $this->assertSame('0',$contents); + } } From 435558c00a55fd496a453d2a9cb89859afd9b2e2 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 21:57:35 +0000 Subject: [PATCH 49/66] Fix styling --- tests/Http/Livewire/SpeciesTable.php | 2 +- tests/TestCase.php | 1 + tests/Views/Columns/CountColumnTest.php | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Http/Livewire/SpeciesTable.php b/tests/Http/Livewire/SpeciesTable.php index d9c1106b9..424034429 100644 --- a/tests/Http/Livewire/SpeciesTable.php +++ b/tests/Http/Livewire/SpeciesTable.php @@ -27,7 +27,7 @@ public function columns(): array ->sortable() ->searchable(), CountColumn::make('Pets') - ->setDataSource('pets'), + ->setDataSource('pets'), ]; } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 6004adf89..ab22227bd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -18,6 +18,7 @@ class TestCase extends Orchestra { public PetsTable $basicTable; + public SpeciesTable $speciesTable; public PetsTableUnpaginated $unpaginatedTable; diff --git a/tests/Views/Columns/CountColumnTest.php b/tests/Views/Columns/CountColumnTest.php index 42c42b641..6c3d1e772 100644 --- a/tests/Views/Columns/CountColumnTest.php +++ b/tests/Views/Columns/CountColumnTest.php @@ -41,10 +41,10 @@ public function test_renders_correctly(): void $rows = $this->speciesTable->getRows(); $row1 = $rows->first(); $column = CountColumn::make('Pets') - ->setDataSource('pets'); + ->setDataSource('pets'); $contents = $column->getContents($rows->first()); - $this->assertSame('2',$contents); + $this->assertSame('2', $contents); $contents = $column->getContents($rows->last()); - $this->assertSame('0',$contents); + $this->assertSame('0', $contents); } } From 6d3e83bc8ca73b5128d8a06bb612f7b318cae779 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:03:21 +0100 Subject: [PATCH 50/66] Add setForeignColumn tests --- .../Configuration/AggregateColumnConfiguration.php | 6 +++--- tests/Views/Columns/AvgColumnTest.php | 14 ++++++++++++++ tests/Views/Columns/SumColumnTest.php | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php index b12c5a3b3..2f181ed2f 100644 --- a/src/Views/Traits/Configuration/AggregateColumnConfiguration.php +++ b/src/Views/Traits/Configuration/AggregateColumnConfiguration.php @@ -9,12 +9,12 @@ trait AggregateColumnConfiguration { public function setDataSource(string $dataSource, ?string $foreignColumn = null): self { + $this->dataSource = $dataSource; + if (isset($foreignColumn)) { - $this->foreignColumn = $foreignColumn; + $this->setForeignColumn($foreignColumn); } - $this->dataSource = $dataSource; - $this->setDefaultLabel(); return $this; diff --git a/tests/Views/Columns/AvgColumnTest.php b/tests/Views/Columns/AvgColumnTest.php index 018eca334..201a532f6 100644 --- a/tests/Views/Columns/AvgColumnTest.php +++ b/tests/Views/Columns/AvgColumnTest.php @@ -60,6 +60,20 @@ public function test_can_get_foreign_column(string $relation_name, string $forei $this->assertSame($foreign_field, $column->getForeignColumn()); } + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_set_foreign_column(string $relation_name, string $foreign_field): void + { + $column = AvgColumn::make('Average Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame($foreign_field, $column->getForeignColumn()); + $column->setForeignColumn('test'); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame('test', $column->getForeignColumn()); + + } + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] public function test_can_get_data_source_fields(string $relation_name, string $foreign_field): void { diff --git a/tests/Views/Columns/SumColumnTest.php b/tests/Views/Columns/SumColumnTest.php index 74af01db5..9fc1605d1 100644 --- a/tests/Views/Columns/SumColumnTest.php +++ b/tests/Views/Columns/SumColumnTest.php @@ -38,6 +38,20 @@ public function test_can_not_skip_set_data_source(string $relation_name, string $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); + + } + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_can_set_foreign_column(string $relation_name, string $foreign_field): void + { + $column = SumColumn::make('Sum User Age') + ->setDataSource($relation_name, $foreign_field) + ->sortable(); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame($foreign_field, $column->getForeignColumn()); + $column->setForeignColumn('test'); + $this->assertTrue($column->hasForeignColumn()); + $this->assertSame('test', $column->getForeignColumn()); + } #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] From c7017f6b8546948482d70e234d1f8cf6ce01e480 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:03:48 +0000 Subject: [PATCH 51/66] Fix styling --- tests/Views/Columns/AvgColumnTest.php | 2 +- tests/Views/Columns/SumColumnTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Views/Columns/AvgColumnTest.php b/tests/Views/Columns/AvgColumnTest.php index 201a532f6..dc151286c 100644 --- a/tests/Views/Columns/AvgColumnTest.php +++ b/tests/Views/Columns/AvgColumnTest.php @@ -71,7 +71,7 @@ public function test_can_set_foreign_column(string $relation_name, string $forei $column->setForeignColumn('test'); $this->assertTrue($column->hasForeignColumn()); $this->assertSame('test', $column->getForeignColumn()); - + } #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] diff --git a/tests/Views/Columns/SumColumnTest.php b/tests/Views/Columns/SumColumnTest.php index 9fc1605d1..f9d282ab9 100644 --- a/tests/Views/Columns/SumColumnTest.php +++ b/tests/Views/Columns/SumColumnTest.php @@ -38,8 +38,8 @@ public function test_can_not_skip_set_data_source(string $relation_name, string $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); - } + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] public function test_can_set_foreign_column(string $relation_name, string $foreign_field): void { @@ -51,7 +51,7 @@ public function test_can_set_foreign_column(string $relation_name, string $forei $column->setForeignColumn('test'); $this->assertTrue($column->hasForeignColumn()); $this->assertSame('test', $column->getForeignColumn()); - + } #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] From e58c1ec7cebefec3311705aefd4b2655e097e7c7 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:07:35 +0100 Subject: [PATCH 52/66] Add ExtraWithAvgs tests --- .../Configuration/ComponentConfigurationTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Traits/Configuration/ComponentConfigurationTest.php b/tests/Traits/Configuration/ComponentConfigurationTest.php index 7000b505e..d1f8d4487 100644 --- a/tests/Traits/Configuration/ComponentConfigurationTest.php +++ b/tests/Traits/Configuration/ComponentConfigurationTest.php @@ -387,4 +387,20 @@ public function test_can_add_extra_with_sum(): void $this->assertTrue($this->basicTable->hasExtraWithSums()); $this->assertSame([['table' => 'users', 'field' => 'age']], $this->basicTable->getExtraWithSums()); } + + public function test_no_extra_with_avgs_by_default(): void + { + $this->assertFalse($this->basicTable->hasExtraWiths()); + $this->assertEmpty($this->basicTable->getExtraWiths()); + } + + public function test_can_add_extra_with_avg(): void + { + $this->assertFalse($this->basicTable->hasExtraWithAvgs()); + $this->assertEmpty($this->basicTable->getExtraWithAvgs()); + $this->basicTable->addExtraWithAvg('user','age'); + $this->assertTrue($this->basicTable->hasExtraWithAvgs()); + $this->assertSame([['table' => 'user', 'field' => 'age']], $this->basicTable->getExtraWithAvgs()); + } + } From efc4521e7967c0a430b249099965abd9b6c77e65 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:08:00 +0000 Subject: [PATCH 53/66] Fix styling --- tests/Traits/Configuration/ComponentConfigurationTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Traits/Configuration/ComponentConfigurationTest.php b/tests/Traits/Configuration/ComponentConfigurationTest.php index d1f8d4487..3bed87443 100644 --- a/tests/Traits/Configuration/ComponentConfigurationTest.php +++ b/tests/Traits/Configuration/ComponentConfigurationTest.php @@ -398,9 +398,8 @@ public function test_can_add_extra_with_avg(): void { $this->assertFalse($this->basicTable->hasExtraWithAvgs()); $this->assertEmpty($this->basicTable->getExtraWithAvgs()); - $this->basicTable->addExtraWithAvg('user','age'); + $this->basicTable->addExtraWithAvg('user', 'age'); $this->assertTrue($this->basicTable->hasExtraWithAvgs()); $this->assertSame([['table' => 'user', 'field' => 'age']], $this->basicTable->getExtraWithAvgs()); } - } From 08de9bd96cd963f32ed678121cb6c98b78b0db88 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:10:00 +0100 Subject: [PATCH 54/66] Update ChangeLog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6993a50e3..7b13aa903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-livewire-tables` will be documented in this file +## UNRELEASED +### New Features +- Add new columns (ArrayColumn, AvgColumn, CountColumn, SumColumn) by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1761 + ## [v3.2.8] - 2024-07-03 ### Bug Fixes - Fix hide bulk actions when empty not reflecting in frontend by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1747 From 351b2a76111c3735e66217ba360a248aac0ecf28 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:30:46 +0100 Subject: [PATCH 55/66] Update Tests for AvgColumn and SumColumn --- .gitignore | 3 +- coverage.xml | 2894 ------------------------- tests/Http/Livewire/SpeciesTable.php | 9 +- tests/Views/Columns/AvgColumnTest.php | 13 + tests/Views/Columns/SumColumnTest.php | 14 + 5 files changed, 36 insertions(+), 2897 deletions(-) delete mode 100644 coverage.xml diff --git a/.gitignore b/.gitignore index a36981422..57913d9f7 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,5 @@ phpunit.xml.dist.dev .history/* .env phpunit.xml.bak -phpstan.txt \ No newline at end of file +phpstan.txt +coverage.xml diff --git a/coverage.xml b/coverage.xml deleted file mode 100644 index 7428459d2..000000000 --- a/coverage.xml +++ /dev/null @@ -1,2894 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/Http/Livewire/SpeciesTable.php b/tests/Http/Livewire/SpeciesTable.php index 424034429..fae1e6214 100644 --- a/tests/Http/Livewire/SpeciesTable.php +++ b/tests/Http/Livewire/SpeciesTable.php @@ -5,7 +5,7 @@ use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Tests\Models\Species; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Columns\CountColumn; +use Rappasoft\LaravelLivewireTables\Views\Columns\{AvgColumn,CountColumn,SumColumn}; class SpeciesTable extends DataTableComponent { @@ -26,8 +26,13 @@ public function columns(): array Column::make('Name') ->sortable() ->searchable(), - CountColumn::make('Pets') + AvgColumn::make('Average Age') + ->setDataSource('pets','age'), + CountColumn::make('Number of Pets') ->setDataSource('pets'), + SumColumn::make('Total Age') + ->setDataSource('pets','age'), + ]; } } diff --git a/tests/Views/Columns/AvgColumnTest.php b/tests/Views/Columns/AvgColumnTest.php index dc151286c..12c34b059 100644 --- a/tests/Views/Columns/AvgColumnTest.php +++ b/tests/Views/Columns/AvgColumnTest.php @@ -96,4 +96,17 @@ public function test_can_get_aggregate_method(string $relation_name, string $for $column->setAggregateMethod('test_avg'); $this->assertSame('test_avg', $column->getAggregateMethod()); } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_renders_correctly(string $relation_name, string $foreign_field): void + { + $rows = $this->speciesTable->getRows(); + $column = AvgColumn::make('Average Age') + ->setDataSource('pets','age'); + $contents = $column->getContents($rows->first()); + $this->assertSame('15', $contents); + $contents = $column->getContents($rows[2]); + $this->assertSame('6', $contents); + } + } diff --git a/tests/Views/Columns/SumColumnTest.php b/tests/Views/Columns/SumColumnTest.php index f9d282ab9..bd045328f 100644 --- a/tests/Views/Columns/SumColumnTest.php +++ b/tests/Views/Columns/SumColumnTest.php @@ -96,4 +96,18 @@ public function test_can_get_aggregate_method(string $relation_name, string $for $column->setAggregateMethod('test_sum'); $this->assertSame('test_sum', $column->getAggregateMethod()); } + + #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')] + public function test_renders_correctly(string $relation_name, string $foreign_field): void + { + $rows = $this->speciesTable->getRows(); + $column = SumColumn::make('Total Age') + ->setDataSource('pets','age'); + $contents = $column->getContents($rows->first()); + $this->assertSame('30', $contents); + $contents = $column->getContents($rows[2]); + $this->assertSame('12', $contents); + } + + } From bf6e12f67b9f3103ee0709466b9ae09007bc9eb7 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:31:12 +0000 Subject: [PATCH 56/66] Fix styling --- tests/Http/Livewire/SpeciesTable.php | 4 ++-- tests/Views/Columns/AvgColumnTest.php | 3 +-- tests/Views/Columns/SumColumnTest.php | 4 +--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/Http/Livewire/SpeciesTable.php b/tests/Http/Livewire/SpeciesTable.php index fae1e6214..5e8c136a2 100644 --- a/tests/Http/Livewire/SpeciesTable.php +++ b/tests/Http/Livewire/SpeciesTable.php @@ -27,11 +27,11 @@ public function columns(): array ->sortable() ->searchable(), AvgColumn::make('Average Age') - ->setDataSource('pets','age'), + ->setDataSource('pets', 'age'), CountColumn::make('Number of Pets') ->setDataSource('pets'), SumColumn::make('Total Age') - ->setDataSource('pets','age'), + ->setDataSource('pets', 'age'), ]; } diff --git a/tests/Views/Columns/AvgColumnTest.php b/tests/Views/Columns/AvgColumnTest.php index 12c34b059..f3657c710 100644 --- a/tests/Views/Columns/AvgColumnTest.php +++ b/tests/Views/Columns/AvgColumnTest.php @@ -102,11 +102,10 @@ public function test_renders_correctly(string $relation_name, string $foreign_fi { $rows = $this->speciesTable->getRows(); $column = AvgColumn::make('Average Age') - ->setDataSource('pets','age'); + ->setDataSource('pets', 'age'); $contents = $column->getContents($rows->first()); $this->assertSame('15', $contents); $contents = $column->getContents($rows[2]); $this->assertSame('6', $contents); } - } diff --git a/tests/Views/Columns/SumColumnTest.php b/tests/Views/Columns/SumColumnTest.php index bd045328f..9d5cbff51 100644 --- a/tests/Views/Columns/SumColumnTest.php +++ b/tests/Views/Columns/SumColumnTest.php @@ -102,12 +102,10 @@ public function test_renders_correctly(string $relation_name, string $foreign_fi { $rows = $this->speciesTable->getRows(); $column = SumColumn::make('Total Age') - ->setDataSource('pets','age'); + ->setDataSource('pets', 'age'); $contents = $column->getContents($rows->first()); $this->assertSame('30', $contents); $contents = $column->getContents($rows[2]); $this->assertSame('12', $contents); } - - } From 994aecd62cf828688b90b02d4ecb866289920378 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:34:47 +0100 Subject: [PATCH 57/66] Add ArrayColumnTest --- .gitignore | 1 + tests/Views/Columns/ArrayColumnTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/Views/Columns/ArrayColumnTest.php diff --git a/.gitignore b/.gitignore index 57913d9f7..d3e4b8ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ phpunit.xml.dist.dev phpunit.xml.bak phpstan.txt coverage.xml +./tmp/** \ No newline at end of file diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php new file mode 100644 index 000000000..04f4a9ff8 --- /dev/null +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -0,0 +1,18 @@ +assertSame('Array Col', $column->getTitle()); + } + +} \ No newline at end of file From dc9b0995b1509e8058c969cf01fc9cd0b6c6c426 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:35:18 +0000 Subject: [PATCH 58/66] Fix styling --- tests/Views/Columns/ArrayColumnTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index 04f4a9ff8..7034d85ee 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -14,5 +14,4 @@ public function test_can_set_the_column_title(): void $this->assertSame('Array Col', $column->getTitle()); } - -} \ No newline at end of file +} From 0bc1c01a7de5c9ff33166877a873bf2f82d9555d Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:43:30 +0100 Subject: [PATCH 59/66] Add ArrayColumnTest --- tests/Views/Columns/ArrayColumnTest.php | 47 ++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index 7034d85ee..f46398686 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -5,6 +5,7 @@ use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Tests\TestCase; use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; final class ArrayColumnTest extends TestCase { @@ -14,4 +15,48 @@ public function test_can_set_the_column_title(): void $this->assertSame('Array Col', $column->getTitle()); } -} + + public function test_can_set_the_separator(): void + { + $column = ArrayColumn::make('Array Col'); + + $this->assertSame('
', $column->getSeparator()); + $column->separator('

'); + $this->assertTrue($column->hasSeparator()); + + $this->assertSame('

', $column->getSeparator()); + } + + public function test_can_set_the_output_format(): void + { + $column = ArrayColumn::make('Array Col'); + + $this->assertNull($column->getOutputFormatCallback()); + $this->assertFalse($column->hasOutputFormatCallback()); + $column->outputFormat(fn($index, $value) => "".$value->name.""); + $this->assertTrue($column->hasOutputFormatCallback()); + } + + public function test_requires_the_data_callback(): void + { + $this->expectException(DataTableConfigurationException::class); + $column = ArrayColumn::make('Average Age') + ->separator('

') + ->sortable(); + $contents = $column->getContents(Pet::find(1)); + $this->assertNull($contents); + } + + + public function test_requires_the_output_format_callback(): void + { + $this->expectException(DataTableConfigurationException::class); + $column = ArrayColumn::make('Average Age') + ->separator('

') + ->data(fn($value, $row) => ($row->pets)) + ->sortable(); + $contents = $column->getContents(Pet::find(1)); + $this->assertNull($contents); + } + +} \ No newline at end of file From 308358ae5d2c0595b82e7530080eaffa3d582b2f Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:44:00 +0000 Subject: [PATCH 60/66] Fix styling --- tests/Views/Columns/ArrayColumnTest.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index f46398686..6870c506e 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -2,10 +2,10 @@ namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Tests\TestCase; use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; final class ArrayColumnTest extends TestCase { @@ -33,7 +33,7 @@ public function test_can_set_the_output_format(): void $this->assertNull($column->getOutputFormatCallback()); $this->assertFalse($column->hasOutputFormatCallback()); - $column->outputFormat(fn($index, $value) => "".$value->name.""); + $column->outputFormat(fn ($index, $value) => "".$value->name.''); $this->assertTrue($column->hasOutputFormatCallback()); } @@ -47,16 +47,14 @@ public function test_requires_the_data_callback(): void $this->assertNull($contents); } - public function test_requires_the_output_format_callback(): void { $this->expectException(DataTableConfigurationException::class); $column = ArrayColumn::make('Average Age') ->separator('

') - ->data(fn($value, $row) => ($row->pets)) + ->data(fn ($value, $row) => ($row->pets)) ->sortable(); $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); } - -} \ No newline at end of file +} From fa128df6f4b19e7bf292aea3f4e1f2ad9285ead7 Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:53:11 +0100 Subject: [PATCH 61/66] Add emptyValue option --- docs/column-types/array_column.md | 10 ++++- .../ArrayColumnConfiguration.php | 11 ++++++ tests/Views/Columns/ArrayColumnTest.php | 37 +++++++++++++++++-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/docs/column-types/array_column.md b/docs/column-types/array_column.md index 69fd002e7..1571da2b9 100644 --- a/docs/column-types/array_column.md +++ b/docs/column-types/array_column.md @@ -6,9 +6,17 @@ weight: 1 Array columns provide an easy way to work with and display an array of data from a field. ``` - ArrayColumn::make('notes', 'name') +ArrayColumn::make('notes', 'name') ->data(fn($value, $row) => ($row->notes)) ->outputFormat(fn($index, $value) => "".$value->name."") ->separator('
') ->sortable(), +``` + +### Empty Value +You may define the default/empty value using the "emptyValue" method + +``` +ArrayColumn::make('notes', 'name') + ->emptyValue('Unknown'), ``` \ No newline at end of file diff --git a/src/Views/Traits/Configuration/ArrayColumnConfiguration.php b/src/Views/Traits/Configuration/ArrayColumnConfiguration.php index 72188fd79..fc7ae5a62 100644 --- a/src/Views/Traits/Configuration/ArrayColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ArrayColumnConfiguration.php @@ -26,4 +26,15 @@ public function outputFormat(callable $callable): self return $this; } + + /** + * Define the Empty Value to use for the Column + */ + public function emptyValue(string $emptyValue): self + { + $this->emptyValue = $emptyValue; + + return $this; + } + } diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index 6870c506e..fc182e553 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -2,10 +2,10 @@ namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Tests\TestCase; use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; final class ArrayColumnTest extends TestCase { @@ -33,7 +33,7 @@ public function test_can_set_the_output_format(): void $this->assertNull($column->getOutputFormatCallback()); $this->assertFalse($column->hasOutputFormatCallback()); - $column->outputFormat(fn ($index, $value) => "".$value->name.''); + $column->outputFormat(fn($index, $value) => "".$value->name.""); $this->assertTrue($column->hasOutputFormatCallback()); } @@ -47,14 +47,43 @@ public function test_requires_the_data_callback(): void $this->assertNull($contents); } + public function test_can_get_the_output_format_callback(): void + { + $this->expectException(DataTableConfigurationException::class); + $column = ArrayColumn::make('Average Age') + ->separator('

') + ->data(fn($value, $row) => ($row->pets)) + ->sortable(); + $this->assertNotNull($column->getDataCallback()); + + $contents = $column->getContents(Pet::find(1)); + $this->assertNull($contents); + } + public function test_requires_the_output_format_callback(): void { $this->expectException(DataTableConfigurationException::class); $column = ArrayColumn::make('Average Age') ->separator('

') - ->data(fn ($value, $row) => ($row->pets)) + ->data(fn($value, $row) => ($row->pets)) ->sortable(); + $contents = $column->getContents(Pet::find(1)); $this->assertNull($contents); } -} + + public function test_can_get_empty_value(): void + { + $column = ArrayColumn::make('Average Age') + ->separator('

') + ->data(fn($value, $row) => ($row->pets)) + ->sortable(); + + $this->assertSame('',$column->getEmptyValue()); + $column->emptyValue('Unknown'); + $this->assertSame('Unknown',$column->getEmptyValue()); + + } + + +} \ No newline at end of file From 1a846bcee48602f5238d09584984ff179e63eb7c Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 22:53:35 +0000 Subject: [PATCH 62/66] Fix styling --- .../Configuration/ArrayColumnConfiguration.php | 1 - tests/Views/Columns/ArrayColumnTest.php | 18 ++++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Views/Traits/Configuration/ArrayColumnConfiguration.php b/src/Views/Traits/Configuration/ArrayColumnConfiguration.php index fc7ae5a62..c0f7657b7 100644 --- a/src/Views/Traits/Configuration/ArrayColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ArrayColumnConfiguration.php @@ -36,5 +36,4 @@ public function emptyValue(string $emptyValue): self return $this; } - } diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index fc182e553..0eff2808b 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -2,10 +2,10 @@ namespace Rappasoft\LaravelLivewireTables\Tests\Views\Columns; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Tests\TestCase; use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; final class ArrayColumnTest extends TestCase { @@ -33,7 +33,7 @@ public function test_can_set_the_output_format(): void $this->assertNull($column->getOutputFormatCallback()); $this->assertFalse($column->hasOutputFormatCallback()); - $column->outputFormat(fn($index, $value) => "".$value->name.""); + $column->outputFormat(fn ($index, $value) => "".$value->name.''); $this->assertTrue($column->hasOutputFormatCallback()); } @@ -52,7 +52,7 @@ public function test_can_get_the_output_format_callback(): void $this->expectException(DataTableConfigurationException::class); $column = ArrayColumn::make('Average Age') ->separator('

') - ->data(fn($value, $row) => ($row->pets)) + ->data(fn ($value, $row) => ($row->pets)) ->sortable(); $this->assertNotNull($column->getDataCallback()); @@ -65,7 +65,7 @@ public function test_requires_the_output_format_callback(): void $this->expectException(DataTableConfigurationException::class); $column = ArrayColumn::make('Average Age') ->separator('

') - ->data(fn($value, $row) => ($row->pets)) + ->data(fn ($value, $row) => ($row->pets)) ->sortable(); $contents = $column->getContents(Pet::find(1)); @@ -76,14 +76,12 @@ public function test_can_get_empty_value(): void { $column = ArrayColumn::make('Average Age') ->separator('

') - ->data(fn($value, $row) => ($row->pets)) + ->data(fn ($value, $row) => ($row->pets)) ->sortable(); - $this->assertSame('',$column->getEmptyValue()); + $this->assertSame('', $column->getEmptyValue()); $column->emptyValue('Unknown'); - $this->assertSame('Unknown',$column->getEmptyValue()); + $this->assertSame('Unknown', $column->getEmptyValue()); } - - -} \ No newline at end of file +} From df7de52fb6e1a86b2d5c295b94126ba2bc25915b Mon Sep 17 00:00:00 2001 From: lrljoe Date: Thu, 11 Jul 2024 00:04:51 +0100 Subject: [PATCH 63/66] Move getContent to Helper method --- src/Views/Columns/AggregateColumn.php | 12 ------------ src/Views/Columns/AvgColumn.php | 13 ------------- src/Views/Columns/CountColumn.php | 13 ------------- src/Views/Columns/SumColumn.php | 13 ------------- src/Views/Traits/Helpers/AggregateColumnHelpers.php | 12 ++++++++++++ src/Views/Traits/Helpers/ArrayColumnHelpers.php | 4 ---- tests/Views/Columns/ArrayColumnTest.php | 1 + 7 files changed, 13 insertions(+), 55 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 3ce175e97..d5ce7bc9a 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -2,9 +2,6 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; @@ -28,13 +25,4 @@ public function __construct(string $title, ?string $from = null) $this->label(fn () => null); } - public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View - { - if (! isset($this->dataSource)) { - throw new DataTableConfigurationException('You must specify a data source'); - } else { - return parent::getContents($row); - } - - } } diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index 07c72c814..eceedd2d6 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -2,9 +2,6 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; @@ -23,14 +20,4 @@ public function __construct(string $title, ?string $from = null) parent::__construct($title, $from); $this->label(fn () => null); } - - public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View - { - if (! isset($this->dataSource)) { - throw new DataTableConfigurationException('You must specify a data source'); - } else { - return parent::getContents($row); - } - - } } diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 21f1777eb..98919f983 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -2,9 +2,6 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; @@ -23,14 +20,4 @@ public function __construct(string $title, ?string $from = null) parent::__construct($title, $from); $this->label(fn () => null); } - - public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View - { - if (! isset($this->dataSource)) { - throw new DataTableConfigurationException('You must specify a data source'); - } else { - return parent::getContents($row); - } - - } } diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 8bfefd52c..02625b1e0 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -2,9 +2,6 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; -use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; @@ -23,14 +20,4 @@ public function __construct(string $title, ?string $from = null) parent::__construct($title, $from); $this->label(fn () => null); } - - public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View - { - if (! isset($this->dataSource)) { - throw new DataTableConfigurationException('You must specify a data source'); - } else { - return parent::getContents($row); - } - - } } diff --git a/src/Views/Traits/Helpers/AggregateColumnHelpers.php b/src/Views/Traits/Helpers/AggregateColumnHelpers.php index 4be7f63b1..ab99092c6 100644 --- a/src/Views/Traits/Helpers/AggregateColumnHelpers.php +++ b/src/Views/Traits/Helpers/AggregateColumnHelpers.php @@ -4,6 +4,8 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\View\ComponentAttributeBag; +use Illuminate\Support\HtmlString; +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; trait AggregateColumnHelpers { @@ -32,4 +34,14 @@ public function getForeignColumn(): string { return $this->foreignColumn; } + + + public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + { + if (! isset($this->dataSource)) { + throw new DataTableConfigurationException('You must specify a data source'); + } else { + return parent::getContents($row); + } + } } diff --git a/src/Views/Traits/Helpers/ArrayColumnHelpers.php b/src/Views/Traits/Helpers/ArrayColumnHelpers.php index 7c32f7c70..8289038e4 100644 --- a/src/Views/Traits/Helpers/ArrayColumnHelpers.php +++ b/src/Views/Traits/Helpers/ArrayColumnHelpers.php @@ -48,10 +48,6 @@ public function getContents(Model $row): null|string|\BackedEnum|HtmlString|Data $outputValues = []; $value = $this->getValue($row); - if (! $this->hasSeparator()) { - throw new DataTableConfigurationException('You must set a valid separator on an ArrayColumn'); - } - if (! $this->hasDataCallback()) { throw new DataTableConfigurationException('You must set a data() method on an ArrayColumn'); } diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index 0eff2808b..431d5721a 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -72,6 +72,7 @@ public function test_requires_the_output_format_callback(): void $this->assertNull($contents); } + public function test_can_get_empty_value(): void { $column = ArrayColumn::make('Average Age') From 94a15f8ed35287e1e3ca4bc7548b4cc80d4cf50e Mon Sep 17 00:00:00 2001 From: lrljoe Date: Wed, 10 Jul 2024 23:05:27 +0000 Subject: [PATCH 64/66] Fix styling --- src/Views/Columns/AggregateColumn.php | 1 - src/Views/Traits/Helpers/AggregateColumnHelpers.php | 3 +-- tests/Views/Columns/ArrayColumnTest.php | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index d5ce7bc9a..d29cac6a2 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -24,5 +24,4 @@ public function __construct(string $title, ?string $from = null) parent::__construct($title, $from); $this->label(fn () => null); } - } diff --git a/src/Views/Traits/Helpers/AggregateColumnHelpers.php b/src/Views/Traits/Helpers/AggregateColumnHelpers.php index ab99092c6..1748b8091 100644 --- a/src/Views/Traits/Helpers/AggregateColumnHelpers.php +++ b/src/Views/Traits/Helpers/AggregateColumnHelpers.php @@ -3,8 +3,8 @@ namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers; use Illuminate\Database\Eloquent\Model; -use Illuminate\View\ComponentAttributeBag; use Illuminate\Support\HtmlString; +use Illuminate\View\ComponentAttributeBag; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; trait AggregateColumnHelpers @@ -35,7 +35,6 @@ public function getForeignColumn(): string return $this->foreignColumn; } - public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { if (! isset($this->dataSource)) { diff --git a/tests/Views/Columns/ArrayColumnTest.php b/tests/Views/Columns/ArrayColumnTest.php index 431d5721a..0eff2808b 100644 --- a/tests/Views/Columns/ArrayColumnTest.php +++ b/tests/Views/Columns/ArrayColumnTest.php @@ -72,7 +72,6 @@ public function test_requires_the_output_format_callback(): void $this->assertNull($contents); } - public function test_can_get_empty_value(): void { $column = ArrayColumn::make('Average Age') From 1e4a25a3c5397554f47488425027df6dfd8aad4b Mon Sep 17 00:00:00 2001 From: lrljoe Date: Thu, 11 Jul 2024 00:12:55 +0100 Subject: [PATCH 65/66] Add IsAggregateColumn trait --- src/Views/Columns/AggregateColumn.php | 9 +++------ src/Views/Columns/AvgColumn.php | 11 +++-------- src/Views/Columns/CountColumn.php | 9 ++------- src/Views/Columns/SumColumn.php | 9 ++------- src/Views/Traits/IsAggregateColumn.php | 15 +++++++++++++++ 5 files changed, 25 insertions(+), 28 deletions(-) create mode 100644 src/Views/Traits/IsAggregateColumn.php diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index d29cac6a2..19e93172a 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -3,15 +3,12 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; -use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Views\Traits\IsAggregateColumn; class AggregateColumn extends Column { - use IsColumn, - AggregateColumnHelpers, - AggregateColumnConfiguration { AggregateColumnConfiguration::sortable insteadof IsColumn; } + use IsAggregateColumn; + public ?string $dataSource; diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index eceedd2d6..c667ac878 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -2,17 +2,12 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; -use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Views\Traits\IsAggregateColumn; class AvgColumn extends AggregateColumn { - use IsColumn, - AggregateColumnHelpers, - AggregateColumnConfiguration { AggregateColumnConfiguration::sortable insteadof IsColumn; } - + use IsAggregateColumn; + public string $aggregateMethod = 'avg'; public function __construct(string $title, ?string $from = null) diff --git a/src/Views/Columns/CountColumn.php b/src/Views/Columns/CountColumn.php index 98919f983..4a1ffd812 100644 --- a/src/Views/Columns/CountColumn.php +++ b/src/Views/Columns/CountColumn.php @@ -2,16 +2,11 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; -use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Views\Traits\IsAggregateColumn; class CountColumn extends AggregateColumn { - use IsColumn, - AggregateColumnHelpers, - AggregateColumnConfiguration { AggregateColumnConfiguration::sortable insteadof IsColumn; } + use IsAggregateColumn; public string $aggregateMethod = 'count'; diff --git a/src/Views/Columns/SumColumn.php b/src/Views/Columns/SumColumn.php index 02625b1e0..a01822d7b 100644 --- a/src/Views/Columns/SumColumn.php +++ b/src/Views/Columns/SumColumn.php @@ -2,16 +2,11 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns; -use Rappasoft\LaravelLivewireTables\Views\Column; -use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\AggregateColumnConfiguration; -use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\AggregateColumnHelpers; -use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn; +use Rappasoft\LaravelLivewireTables\Views\Traits\IsAggregateColumn; class SumColumn extends AggregateColumn { - use IsColumn, - AggregateColumnHelpers, - AggregateColumnConfiguration { AggregateColumnConfiguration::sortable insteadof IsColumn; } + use IsAggregateColumn; public string $aggregateMethod = 'sum'; diff --git a/src/Views/Traits/IsAggregateColumn.php b/src/Views/Traits/IsAggregateColumn.php new file mode 100644 index 000000000..9e242ae18 --- /dev/null +++ b/src/Views/Traits/IsAggregateColumn.php @@ -0,0 +1,15 @@ + Date: Wed, 10 Jul 2024 23:13:23 +0000 Subject: [PATCH 66/66] Fix styling --- src/Views/Columns/AggregateColumn.php | 1 - src/Views/Columns/AvgColumn.php | 2 +- src/Views/Traits/IsAggregateColumn.php | 7 +++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Views/Columns/AggregateColumn.php b/src/Views/Columns/AggregateColumn.php index 19e93172a..a284b08ff 100644 --- a/src/Views/Columns/AggregateColumn.php +++ b/src/Views/Columns/AggregateColumn.php @@ -9,7 +9,6 @@ class AggregateColumn extends Column { use IsAggregateColumn; - public ?string $dataSource; public string $aggregateMethod = 'count'; diff --git a/src/Views/Columns/AvgColumn.php b/src/Views/Columns/AvgColumn.php index c667ac878..3af197873 100644 --- a/src/Views/Columns/AvgColumn.php +++ b/src/Views/Columns/AvgColumn.php @@ -7,7 +7,7 @@ class AvgColumn extends AggregateColumn { use IsAggregateColumn; - + public string $aggregateMethod = 'avg'; public function __construct(string $title, ?string $from = null) diff --git a/src/Views/Traits/IsAggregateColumn.php b/src/Views/Traits/IsAggregateColumn.php index 9e242ae18..1824118b9 100644 --- a/src/Views/Traits/IsAggregateColumn.php +++ b/src/Views/Traits/IsAggregateColumn.php @@ -9,7 +9,6 @@ trait IsAggregateColumn { use IsColumn, AggregateColumnHelpers, - AggregateColumnConfiguration { AggregateColumnHelpers::getContents insteadof IsColumn; AggregateColumnConfiguration::sortable insteadof IsColumn; } - - -} \ No newline at end of file + AggregateColumnConfiguration { AggregateColumnHelpers::getContents insteadof IsColumn; + AggregateColumnConfiguration::sortable insteadof IsColumn; } +}