Skip to content

Commit 025093b

Browse files
committed
Added getFiltersWithoutSearch method
- Updated getFilter to check the type first - Added getFiltersWithoutSearch method - Refactored all views to use getFiltersWithoutSearch
1 parent 9438a25 commit 025093b

File tree

7 files changed

+29
-15
lines changed

7 files changed

+29
-15
lines changed

resources/views/bootstrap-4/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showFilters && count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
1+
@if ($showFilters && count($this->getFiltersWithoutSearch()))
22
<div class="mb-3">
33
<small>@lang('Applied Filters'):</small>
44

resources/views/bootstrap-4/includes/filters.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<button type="button" class="btn dropdown-toggle d-block w-100 d-md-inline" data-toggle="dropdown">
44
@lang('Filters')
55

6-
@if (count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
6+
@if (count($this->getFiltersWithoutSearch()))
77
<span class="badge badge-info">
8-
{{ isset($filters['search']) ? count($this->getFilters()) - 1 : count($this->getFilters()) }}
8+
{{ count($this->getFiltersWithoutSearch()) }}
99
</span>
1010
@endif
1111

@@ -38,7 +38,7 @@ class="form-control"
3838
@endforeach
3939
@endif
4040

41-
@if (count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
41+
@if (count($this->getFiltersWithoutSearch()))
4242
<div class="dropdown-divider"></div>
4343

4444
<a

resources/views/bootstrap-5/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showFilters && count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
1+
@if ($showFilters && count($this->getFiltersWithoutSearch()))
22
<div class="mb-3">
33
<small>@lang('Applied Filters'):</small>
44

resources/views/bootstrap-5/includes/filters.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<button type="button" class="btn dropdown-toggle d-block w-100 d-md-inline" data-bs-toggle="dropdown">
44
@lang('Filters')
55

6-
@if (count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
6+
@if (count($this->getFiltersWithoutSearch()))
77
<span class="badge bg-info">
8-
{{ isset($filters['search']) ? count($this->getFilters()) - 1 : count($this->getFilters()) }}
8+
{{ count($this->getFiltersWithoutSearch()) }}
99
</span>
1010
@endif
1111

@@ -38,7 +38,7 @@ class="form-control"
3838
@endforeach
3939
@endif
4040

41-
@if (count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
41+
@if (count($this->getFiltersWithoutSearch()))
4242
<div class="dropdown-divider"></div>
4343

4444
<a

resources/views/tailwind/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showFilters && count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
1+
@if ($showFilters && count($this->getFiltersWithoutSearch()))
22
<div class="p-6 md:p-0">
33
<small class="text-gray-700">@lang('Applied Filters'):</small>
44

resources/views/tailwind/includes/filters.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class="relative block md:inline-block text-left"
1111
class="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:border-indigo-300 focus:shadow-outline-indigo" id="filters-menu" @click="open = !open" aria-haspopup="true" x-bind:aria-expanded="open" aria-expanded="true">
1212
@lang('Filters')
1313

14-
@if (count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
14+
@if (count($this->getFiltersWithoutSearch()))
1515
<span class="ml-1 inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium leading-4 bg-indigo-100 text-indigo-800 capitalize">
16-
{{ isset($filters['search']) ? count($this->getFilters()) - 1 : count($this->getFilters()) }}
16+
{{ count($this->getFiltersWithoutSearch()) }}
1717
</span>
1818
@endif
1919

@@ -66,7 +66,7 @@ class="rounded-md shadow-sm block w-full pl-3 pr-10 py-2 text-base leading-6 bor
6666
@endforeach
6767
@endif
6868

69-
@if (count($this->getFilters()) && !(count($this->getFilters()) === 1 && isset($filters['search'])))
69+
@if (count($this->getFiltersWithoutSearch()))
7070
<div class="py-1" role="none">
7171
<div class="block px-4 py-2 text-sm text-gray-700" role="menuitem">
7272
<button

src/Traits/WithFilters.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,16 @@ public function hasFilter(string $filter): bool
165165
*
166166
* @param string $filter
167167
*
168-
* @return int|mixed|null
168+
* @return int|string|null
169169
*/
170170
public function getFilter(string $filter)
171171
{
172172
if ($this->hasFilter($filter)) {
173-
return $this->hasIntegerKeys($filter) ? (int)$this->filters[$filter] : $this->filters[$filter];
173+
if (in_array($filter, collect($this->filters())->keys()->toArray(), true) && $this->filters()[$filter]->isSelect()) {
174+
return $this->hasIntegerKeys($filter) ? (int)$this->filters[$filter] : trim($this->filters[$filter]);
175+
}
176+
177+
return trim($this->filters[$filter]);
174178
}
175179

176180
return null;
@@ -186,6 +190,16 @@ public function getFilters(): array
186190
->toArray();
187191
}
188192

193+
/**
194+
* @return array
195+
*/
196+
public function getFiltersWithoutSearch(): array
197+
{
198+
return collect($this->getFilters())
199+
->reject(fn ($_value, $key) => $key === 'search')
200+
->toArray();
201+
}
202+
189203
/**
190204
* Set a given filter to null
191205
*
@@ -247,7 +261,7 @@ public function applySearchFilter(Builder $query): Builder
247261
$searchableColumns = $this->getSearchableColumns();
248262

249263
if ($this->hasFilter('search') && count($searchableColumns)) {
250-
$search = trim($this->getFilter('search'));
264+
$search = $this->getFilter('search');
251265

252266
// Group search conditions together
253267
$query->where(function (Builder $subQuery) use ($search, $query, $searchableColumns) {

0 commit comments

Comments
 (0)