Skip to content

Commit

Permalink
Merge pull request #50 from pxlrbt/feature/case-insensitive-search
Browse files Browse the repository at this point in the history
Feature/case insensitive search
  • Loading branch information
pxlrbt authored Nov 18, 2024
2 parents 860e1d3 + 78dacd3 commit 85e6f00
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;
use LivewireUI\Spotlight\SpotlightSearchResult;

use function Filament\Support\generate_search_column_expression;

class ResourceCommand extends SpotlightCommand
{
protected Resource $resource;
Expand Down Expand Up @@ -103,7 +104,7 @@ public function searchRecord($query): EloquentCollection|Collection|array
$isFirst = true;

foreach ($resource::getGloballySearchableAttributes() as $attributes) {
static::applyGlobalSearchAttributeConstraint($query, Arr::wrap($attributes), $searchQueryWord, $isFirst);
static::applyGlobalSearchAttributeConstraint($query, Arr::wrap($attributes), $searchQueryWord, $isFirst, $resource);
}
});
}
Expand All @@ -120,30 +121,33 @@ public function searchRecord($query): EloquentCollection|Collection|array
));
}

protected static function applyGlobalSearchAttributeConstraint(Builder $query, array $searchAttributes, string $searchQuery, bool &$isFirst): Builder
protected static function applyGlobalSearchAttributeConstraint(Builder $query, array $searchAttributes, string $searchQuery, bool &$isFirst, $resource): Builder
{
$isForcedCaseInsensitive = $resource::isGlobalSearchForcedCaseInsensitive();

/** @var Connection $databaseConnection */
$databaseConnection = $query->getConnection();

$searchOperator = match ($databaseConnection->getDriverName()) {
'pgsql' => 'ilike',
default => 'like',
};
if ($isForcedCaseInsensitive) {
$searchQuery = strtolower($searchQuery);
}

foreach ($searchAttributes as $searchAttribute) {
$whereClause = $isFirst ? 'where' : 'orWhere';

$query->when(
Str::of($searchAttribute)->contains('.'),
fn ($query) => $query->{"{$whereClause}Relation"}(
(string) Str::of($searchAttribute)->beforeLast('.'),
(string) Str::of($searchAttribute)->afterLast('.'),
$searchOperator,
"%{$searchQuery}%",
),
fn ($query) => $query->{$whereClause}(
$searchAttribute,
$searchOperator,
str($searchAttribute)->contains('.'),
function (Builder $query) use ($databaseConnection, $isForcedCaseInsensitive, $searchAttribute, $searchQuery, $whereClause): Builder {
return $query->{"{$whereClause}Relation"}(
(string) str($searchAttribute)->beforeLast('.'),
generate_search_column_expression((string) str($searchAttribute)->afterLast('.'), $isForcedCaseInsensitive, $databaseConnection),
'like',
"%{$searchQuery}%",
);
},
fn (Builder $query) => $query->{$whereClause}(
generate_search_column_expression($searchAttribute, $isForcedCaseInsensitive, $databaseConnection),
'like',
"%{$searchQuery}%",
),
);
Expand Down

0 comments on commit 85e6f00

Please sign in to comment.