From 8c0b39ad406ad76fd80445a87b336557b6d8c366 Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Tue, 6 Jul 2021 01:28:57 +1000 Subject: [PATCH] Downcase attribute and query for case-insensitive search (#493) --- src/Engines/CollectionEngine.php | 2 +- tests/Feature/CollectionEngineTest.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Engines/CollectionEngine.php b/src/Engines/CollectionEngine.php index 72e48cfb..a9c96f27 100644 --- a/src/Engines/CollectionEngine.php +++ b/src/Engines/CollectionEngine.php @@ -117,7 +117,7 @@ protected function searchModels(Builder $builder) foreach ($columns as $column) { $attribute = $model->{$column}; - if (Str::contains($attribute, $builder->query)) { + if (Str::contains(Str::lower($attribute), Str::lower($builder->query))) { return true; } } diff --git a/tests/Feature/CollectionEngineTest.php b/tests/Feature/CollectionEngineTest.php index db1fc129..d4cfa2eb 100644 --- a/tests/Feature/CollectionEngineTest.php +++ b/tests/Feature/CollectionEngineTest.php @@ -54,6 +54,9 @@ public function test_it_can_retrieve_results() $models = SearchableUserModel::search('Taylor')->where('email', 'taylor@laravel.com')->get(); $this->assertCount(1, $models); + $models = SearchableUserModel::search('otwell')->get(); + $this->assertCount(2, $models); + $models = SearchableUserModel::search('laravel')->get(); $this->assertCount(2, $models);