Skip to content

Commit

Permalink
fix filters
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Sep 25, 2023
1 parent 7074b1c commit 9999fa3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Table/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cone\Root\Table\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

Expand All @@ -31,14 +32,26 @@ public function __construct(Table $table, array $filters = [])
$this->filters = new Collection($filters);
}

/**
* Register the given filters.
*/
public function register(array|Filter $filters): static
{
foreach (Arr::wrap($filters) as $filter) {
$this->filters->push($filter);
}

return $this;
}

/**
* Make a new filter instance.
*/
public function filter(string $filter, ...$params): Filter
{
$instance = new $filter($this->table, ...$params);

$this->push($instance);
$this->register($instance);

return $instance;
}
Expand All @@ -49,7 +62,7 @@ public function filter(string $filter, ...$params): Filter
public function apply(Request $request, Builder $query): Builder
{
$this->filters->filter(static function (Filter $filter) use ($request): bool {
return $request->has($filter->getKey());
return $request->has($filter->getRequestKey());
})->each(static function (Filter $filter) use ($query, $request): void {
$filter->apply($request, $query, $filter->getValue($request));
});
Expand Down

0 comments on commit 9999fa3

Please sign in to comment.