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 1/3] 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}%", ), ); From 472a03bee42736fa9abe290aff735e6222d43981 Mon Sep 17 00:00:00 2001 From: Dennis Koch Date: Mon, 18 Nov 2024 22:15:21 +0800 Subject: [PATCH 2/3] Cleanup refactoring --- src/Commands/ResourceCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index 283c97b..b672b1a 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -103,7 +103,6 @@ public function searchRecord($query): EloquentCollection|Collection|array $isFirst = true; foreach ($resource::getGloballySearchableAttributes() as $attributes) { - //$resource::applyGlobalSearchAttributeConstraint($query, $searchQueryWord,Arr::wrap($attributes), $isFirst); static::applyGlobalSearchAttributeConstraint($query, Arr::wrap($attributes), $searchQueryWord, $isFirst, $resource); } }); @@ -123,15 +122,15 @@ public function searchRecord($query): EloquentCollection|Collection|array protected static function applyGlobalSearchAttributeConstraint(Builder $query, array $searchAttributes, string $searchQuery, bool &$isFirst, $resource): Builder { - $model = $query->getModel(); - $isForcedCaseInsensitive = $resource::isGlobalSearchForcedCaseInsensitive(); /** @var Connection $databaseConnection */ $databaseConnection = $query->getConnection(); + if ($isForcedCaseInsensitive){ - $searchQuery = strtolower($searchQuery); + $searchQuery = strtolower($searchQuery); } + foreach ($searchAttributes as $searchAttribute) { $whereClause = $isFirst ? 'where' : 'orWhere'; From 2a3504cc4d73d7b547704d358b2ec6c07c0dafb6 Mon Sep 17 00:00:00 2001 From: pxlrbt Date: Mon, 18 Nov 2024 14:17:01 +0000 Subject: [PATCH 3/3] Apply style changes --- src/Actions/RegisterPages.php | 2 +- src/Actions/RegisterUserMenu.php | 2 +- src/Commands/ResourceCommand.php | 9 ++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Actions/RegisterPages.php b/src/Actions/RegisterPages.php index 895a2c8..633f1f6 100644 --- a/src/Actions/RegisterPages.php +++ b/src/Actions/RegisterPages.php @@ -18,7 +18,7 @@ public static function boot(Panel $panel) /** * @var Page $page */ - $page = new $pageClass(); + $page = new $pageClass; if (method_exists($page, 'shouldRegisterSpotlight') && $page::shouldRegisterSpotlight() === false) { continue; diff --git a/src/Actions/RegisterUserMenu.php b/src/Actions/RegisterUserMenu.php index f67effd..f7bb853 100644 --- a/src/Actions/RegisterUserMenu.php +++ b/src/Actions/RegisterUserMenu.php @@ -12,7 +12,7 @@ class RegisterUserMenu { public static function boot(Panel $panel) { - $self = new static(); + $self = new static; /** * @var array $items */ diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index b672b1a..3a1a381 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -14,12 +14,12 @@ 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 @@ -37,8 +37,8 @@ public function __construct( string $page, protected string $key, ) { - $this->resource = new $resource(); - $this->page = new $page(); + $this->resource = new $resource; + $this->page = new $page; } public function getId(): string @@ -97,7 +97,6 @@ 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; @@ -127,7 +126,7 @@ protected static function applyGlobalSearchAttributeConstraint(Builder $query, a /** @var Connection $databaseConnection */ $databaseConnection = $query->getConnection(); - if ($isForcedCaseInsensitive){ + if ($isForcedCaseInsensitive) { $searchQuery = strtolower($searchQuery); }