Skip to content

Commit

Permalink
Deduplicate cmsSearchLogic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Jul 11, 2024
1 parent 1577a1e commit 0473dbc
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/Repositories/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use A17\Twill\Repositories\Behaviors\HandlePermissions;
use A17\Twill\Repositories\Behaviors\HandleRelatedBrowsers;
use A17\Twill\Repositories\Behaviors\HandleRepeaters;
use A17\Twill\Services\Listings\Filters\FreeTextSearch;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -152,19 +153,16 @@ public function listAll(
return $query->get()->pluck($column, $pluckBy);
}

public function cmsSearch(string $search, array $fields = []): Collection
public function cmsSearch(string $search, array $fields = [], callable $query = null): Collection
{
$builder = $this->model->latest();
$searchFilter = new FreeTextSearch();
$searchFilter->queryString($search);
$searchFilter->searchColumns($fields);
$searchFilter->searchQuery($query);

$translatedAttributes = $this->model->getTranslatedAttributes() ?? [];
$builder = $this->model->latest();

foreach ($fields as $field) {
if (in_array($field, $translatedAttributes, true)) {
$builder->orWhereTranslationLike($field, "%$search%");
} else {
$builder->orWhere($field, getLikeOperator(), "%$search%");
}
}
$searchFilter->applyFilter($builder);

return $builder->get();
}
Expand Down

0 comments on commit 0473dbc

Please sign in to comment.