From f761a41acafd7803b23b025a5ae65caee7bf0e54 Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 23 May 2024 13:01:51 +0200 Subject: [PATCH] Improve code --- src/Concerns/Filter.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Concerns/Filter.php b/src/Concerns/Filter.php index 6b85281e..72357070 100644 --- a/src/Concerns/Filter.php +++ b/src/Concerns/Filter.php @@ -5,6 +5,7 @@ use DateTimeZone; use Illuminate\Support\{Arr, Carbon, Collection}; use Livewire\Attributes\On; +use PowerComponents\LivewirePowerGrid\Column; trait Filter { @@ -330,19 +331,21 @@ public function addEnabledFilters(string $field, ?string $label): void } } - //@TODO REFACTOR - public function ____COLUMNS_FOR_QUERY_STRING(): Collection + public function listColumnForFilters(): Collection { + $columns = collect(); + collect($this->columns()) - ->each(function ($column) use (&$columns) { - if (isset($column->dataField)) { - $columns[$column->dataField] = $column->title ?? $column->dataField; - } + ->ensure([Column::class]) + ->each(function ($column) use (&$columns) { + if (isset($column->dataField)) { + $columns->put($column->dataField, $column->title ?? $column->dataField); + } - $columns[$column->field] = $column->title ?? $column->field; - }); + $columns->put($column->field, $column->title ?? $column->field); + }); - return collect($columns); + return $columns; } /* @@ -352,12 +355,12 @@ protected function powerGridQueryString(): array { $queryString = []; - $columns = $this->____COLUMNS_FOR_QUERY_STRING(); + $columns = $this->listColumnForFilters(); foreach (Arr::dot($this->filters()) as $filter) { $as = str($filter->field)->replace('.', '_'); - if (!is_null(request()->get($as))) { + if (!empty(request()->get($as))) { $this->addEnabledFilters($filter->field, strval($columns->get($filter->field, $filter->field))); }