Skip to content
32 changes: 31 additions & 1 deletion src/Traits/SearchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ trait SearchTrait
protected $attachedRelations = [];
protected $attachedRelationsCount = [];

protected $reservedFilters = [
'with',
'with_count',
'with_trashed',
'query',
'order_by',
'all',
'per_page',
'page',
'desc'
];

public function paginate(): LengthAwarePaginator
{
$defaultPerPage = config('defaults.items_per_page');
Expand Down Expand Up @@ -84,7 +96,7 @@ public function filterByQuery(array $fields): self
return $this;
}

public function searchQuery(array $filter): self
public function searchQuery(array $filter = []): self
{
if (!empty($filter['with_trashed'])) {
$this->withTrashed();
Expand All @@ -97,6 +109,24 @@ public function searchQuery(array $filter): self

$this->filter = $filter;

foreach($filter as $fieldName => $value) {
$isNotReservedFilter = (!in_array($fieldName, $this->reservedFilters));

if (Str::endsWith($fieldName, '_not_in_list')) {
$field = Str::replace('_not_in_list', '', $fieldName);
$this->query->whereNotIn($field, $value);
} elseif (Str::endsWith($fieldName, '_from')) {
$field = Str::replace('_from', '', $fieldName);
$this->filterFrom($field, false, $fieldName);
} elseif (Str::endsWith($fieldName, '_to')) {
$field = Str::replace('_to', '', $fieldName);
$this->filterTo($field, false, $fieldName);
} elseif ($isNotReservedFilter || Str::endsWith($fieldName, '_in_list')) {
$field = Str::replace('_in_list', '', $fieldName);
$this->filterBy($field);
}
}

return $this;
}

Expand Down