Skip to content

Commit a9d8775

Browse files
authored
Support using null-safe operator with null value (#55175)
* Add test * Mark `<=>` operator as valid for `null` comparisons
1 parent 637124b commit a9d8775

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
866866
// where null clause to the query. So, we will allow a short-cut here to
867867
// that method for convenience so the developer doesn't have to check.
868868
if (is_null($value)) {
869-
return $this->whereNull($column, $boolean, $operator !== '=');
869+
return $this->whereNull($column, $boolean, ! in_array($operator, ['=', '<=>'], true));
870870
}
871871

872872
$type = 'Basic';
@@ -958,7 +958,7 @@ public function prepareValueAndOperator($value, $operator, $useDefault = false)
958958
protected function invalidOperatorAndValue($operator, $value)
959959
{
960960
return is_null($value) && in_array($operator, $this->operators) &&
961-
! in_array($operator, ['=', '<>', '!=']);
961+
! in_array($operator, ['=', '<=>', '<>', '!=']);
962962
}
963963

964964
/**

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5030,6 +5030,10 @@ public function testProvidingNullWithOperatorsBuildsCorrectly()
50305030
$builder = $this->getBuilder();
50315031
$builder->select('*')->from('users')->where('foo', '<>', null);
50325032
$this->assertSame('select * from "users" where "foo" is not null', $builder->toSql());
5033+
5034+
$builder = $this->getBuilder();
5035+
$builder->select('*')->from('users')->where('foo', '<=>', null);
5036+
$this->assertSame('select * from "users" where "foo" is null', $builder->toSql());
50335037
}
50345038

50355039
public function testDynamicWhere()

0 commit comments

Comments
 (0)