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

fix filtering with operator not like issue #1829

Merged
merged 3 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add additional test case for not like here please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance you could add a test as well?

I added the test case and replaced the string operation as suggested.

if ($operator === 'not like') {
$operator = 'not';
} else {
$operator = '=';
}

// Convert to regular expression.
$regex = preg_replace('#(^|[^\\\])%#', '$1.*', preg_quote($value));
Expand Down
15 changes: 15 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down