From be9c80507646840018ce7d251547254eef928941 Mon Sep 17 00:00:00 2001 From: oooiik Date: Tue, 8 Nov 2022 13:39:54 +0500 Subject: [PATCH] default --- src/Filters/QueryFilter.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Filters/QueryFilter.php b/src/Filters/QueryFilter.php index ad9208b..93bdf86 100644 --- a/src/Filters/QueryFilter.php +++ b/src/Filters/QueryFilter.php @@ -2,6 +2,7 @@ namespace Oooiik\LaravelQueryFilter\Filters; +use http\Exception\BadMethodCallException; use Illuminate\Database\Eloquent\Builder; abstract class QueryFilter @@ -11,6 +12,8 @@ abstract class QueryFilter /** @var Builder */ protected $realBuilder; + public array $default = []; + public function __construct(Builder $builder) { $this->realBuilder = $builder; @@ -31,9 +34,13 @@ public function filters() public function apply(array $validated) { + $validatedKeys = array_keys($validated); + $defaultKeys = array_keys($this->default); foreach ($this->filters() as $filter) { - if (in_array($filter, array_keys($validated))) { + if (in_array($filter, $validatedKeys)) { $this->$filter($validated[$filter], $validated); + } elseif (in_array($filter, $defaultKeys)) { + $this->$filter($this->default[$filter], $validated); } } return $this;