Skip to content

Instantly share code, notes, and snippets.

@zalviandyr
Created May 17, 2023 10:33
Show Gist options
  • Save zalviandyr/c0d00e46f51404db37790787cb1a4937 to your computer and use it in GitHub Desktop.
Save zalviandyr/c0d00e46f51404db37790787cb1a4937 to your computer and use it in GitHub Desktop.
trait AutoSearch
{
public function scopeAutoSearch(Builder $query, ?string $keyword)
{
if ($keyword !== null) {
$dbDriver = DB::connection()->getConfig('driver');
if ($dbDriver === 'pgsql') {
$columns = Arr::map($this->searchableColumns, fn ($elm) => $elm.'::text');
$query->whereLike($columns, $keyword);
} else {
$query->whereLike($this->searchableColumns, $keyword);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment