Skip to content

Support using null-safe operator with null value #55175

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

Merged
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
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
// where null clause to the query. So, we will allow a short-cut here to
// that method for convenience so the developer doesn't have to check.
if (is_null($value)) {
return $this->whereNull($column, $boolean, $operator !== '=');
return $this->whereNull($column, $boolean, ! in_array($operator, ['=', '<=>'], true));
}

$type = 'Basic';
Expand Down Expand Up @@ -958,7 +958,7 @@ public function prepareValueAndOperator($value, $operator, $useDefault = false)
protected function invalidOperatorAndValue($operator, $value)
{
return is_null($value) && in_array($operator, $this->operators) &&
! in_array($operator, ['=', '<>', '!=']);
! in_array($operator, ['=', '<=>', '<>', '!=']);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5030,6 +5030,10 @@ public function testProvidingNullWithOperatorsBuildsCorrectly()
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('foo', '<>', null);
$this->assertSame('select * from "users" where "foo" is not null', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('foo', '<=>', null);
$this->assertSame('select * from "users" where "foo" is null', $builder->toSql());
}

public function testDynamicWhere()
Expand Down
Loading