Skip to content

Commit

Permalink
Use double quotes to allow searching string with single quotes in them
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkirkosa committed Nov 22, 2023
1 parent 318e86e commit c0d9b4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Filters/SearchableFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public function filter(RestifyRequest $request, $query, $value)
}

if (! config('restify.search.case_sensitive')) {
return $query->orWhereRaw("UPPER({$this->column}) LIKE '%".strtoupper($value)."%'");
$upper = strtoupper($value);

return $query->orWhereRaw("UPPER({$this->column}) LIKE \"%{$upper}%\"");
}

return $query->orWhere($this->column, $likeOperator, '%'.$value.'%');
return $query->orWhere($this->column, $likeOperator, "%{$value}%");
}

public function usingBelongsTo(BelongsTo $field): self
Expand Down
22 changes: 21 additions & 1 deletion tests/Feature/RepositorySearchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function test_can_search_using_filter_searchable_definition(): void
$this->getJson(UserRepository::route(query: ['search' => 'John']))->assertJsonCount(4, 'data');
}

public function test_can_search_incase_sensitive(): void
public function test_can_search_case_insensitive(): void
{
config()->set('restify.search.case_sensitive', false);

Expand All @@ -50,6 +50,26 @@ public function test_can_search_incase_sensitive(): void
$this->getJson(UserRepository::route(query: ['search' => 'John']))->assertJsonCount(4, 'data');
}

public function test_search_correctly_using_quotes(): void
{
config()->set('restify.search.case_sensitive', false);

User::factory(4)->create([
'name' => "Brian O'Donnel",
]);

User::factory(4)->create([
'name' => 'wew',
]);

UserRepository::$search = [
'name',
];

$this->getJson(UserRepository::route(query: ['search' => "O'Donnel"]))
->assertJsonCount(4, 'data');
}

public function test_can_search_using_belongs_to_field(): void
{
$foreignUser = User::factory()->create([
Expand Down

0 comments on commit c0d9b4a

Please sign in to comment.