From 9955742f259306aeaa18c3f6b5561292f13fe7bc Mon Sep 17 00:00:00 2001 From: Geoffry Van den Eede Date: Fri, 29 Mar 2024 09:29:46 +0100 Subject: [PATCH] Update ResourceCommand.php Add the force case insensitive to make the search case insensitive for searching in json fields, for example translation fields. Copied the code from the Filament Resource and added the current resource to the function --- src/Commands/ResourceCommand.php | 42 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index 3511217..283c97b 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -20,6 +20,7 @@ use LivewireUI\Spotlight\SpotlightCommandDependencies; use LivewireUI\Spotlight\SpotlightCommandDependency; use LivewireUI\Spotlight\SpotlightSearchResult; +use function Filament\Support\generate_search_column_expression; class ResourceCommand extends SpotlightCommand { @@ -96,12 +97,14 @@ public function searchRecord($query): EloquentCollection|Collection|array $searchQuery = $query; $query = $resource::getEloquentQuery(); + foreach (explode(' ', $searchQuery) as $searchQueryWord) { $query->where(function (Builder $query) use ($searchQueryWord, $resource) { $isFirst = true; foreach ($resource::getGloballySearchableAttributes() as $attributes) { - static::applyGlobalSearchAttributeConstraint($query, Arr::wrap($attributes), $searchQueryWord, $isFirst); + //$resource::applyGlobalSearchAttributeConstraint($query, $searchQueryWord,Arr::wrap($attributes), $isFirst); + static::applyGlobalSearchAttributeConstraint($query, Arr::wrap($attributes), $searchQueryWord, $isFirst, $resource); } }); } @@ -118,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 { - /** @var Connection $databaseConnection */ - $databaseConnection = $query->getConnection(); + $model = $query->getModel(); - $searchOperator = match ($databaseConnection->getDriverName()) { - 'pgsql' => 'ilike', - default => 'like', - }; + $isForcedCaseInsensitive = $resource::isGlobalSearchForcedCaseInsensitive(); + /** @var Connection $databaseConnection */ + $databaseConnection = $query->getConnection(); + 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}%", ), );