Skip to content

Commit

Permalink
Update ResourceCommand.php
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Geoffry304 authored Mar 29, 2024
1 parent 138f1cf commit 9955742
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
});
}
Expand All @@ -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}%",
),
);
Expand Down

0 comments on commit 9955742

Please sign in to comment.