From 30510ec9d80e64757012a0925fcb96ee34d0f3f3 Mon Sep 17 00:00:00 2001 From: Michael Nardolillo Date: Fri, 13 Sep 2019 03:20:45 +0200 Subject: [PATCH 1/3] fix filtering with operator not like issue --- src/Jenssegers/Mongodb/Query/Builder.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index c706b24a4..9752368e6 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -978,9 +978,13 @@ protected function compileWhereBasic(array $where) { extract($where); - // Replace like with a Regex instance. - if ($operator == 'like') { - $operator = '='; + // Replace like or not like with a Regex instance. + if (in_array($operator, ['like', 'not like'])) { + if (Str::startsWith($operator, 'not')) { + $operator = 'not'; + } else { + $operator = '='; + } // Convert to regular expression. $regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value)); From 05bdd8127ab2aaa1112dd901bfa93955c30e15ab Mon Sep 17 00:00:00 2001 From: Michael Nardolillo Date: Fri, 13 Sep 2019 11:23:29 +0200 Subject: [PATCH 2/3] replcae string operation with exact match of (not like) --- src/Jenssegers/Mongodb/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 9752368e6..70b857293 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -980,7 +980,7 @@ protected function compileWhereBasic(array $where) // Replace like or not like with a Regex instance. if (in_array($operator, ['like', 'not like'])) { - if (Str::startsWith($operator, 'not')) { + if ($operator === 'not like') { $operator = 'not'; } else { $operator = '='; From e2c97629d552d8f9f69ae2fdd97fdc7dfcd170e7 Mon Sep 17 00:00:00 2001 From: Michael Nardolillo Date: Fri, 13 Sep 2019 11:25:08 +0200 Subject: [PATCH 3/3] add test to not like oepration fix --- tests/QueryTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index b697555cc..ab36d1886 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -71,6 +71,21 @@ public function testLike(): void $this->assertCount(1, $users); } + public function testNotLike(): void + { + $users = User::where('name', 'not like', '%doe')->get(); + $this->assertCount(7, $users); + + $users = User::where('name', 'not like', '%y%')->get(); + $this->assertCount(6, $users); + + $users = User::where('name', 'not LIKE', '%y%')->get(); + $this->assertCount(6, $users); + + $users = User::where('name', 'not like', 't%')->get(); + $this->assertCount(8, $users); + } + public function testSelect(): void { $user = User::where('name', 'John Doe')->select('name')->first();