Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ResourceCommand.php #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading