Skip to content

Commit c669a83

Browse files
Allow for nullable morphs in whereNotMorphedTo (#42878)
* use the null safe operator to return null polymorphic relationships
1 parent f4032f8 commit c669a83

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,12 @@ public function whereNotMorphedTo($relation, $model, $boolean = 'and')
493493
$model = array_search($model, $morphMap, true);
494494
}
495495

496-
return $this->whereNot($relation->getMorphType(), $model, null, $boolean);
496+
return $this->whereNot($relation->getMorphType(), '<=>', $model, $boolean);
497497
}
498498

499499
return $this->whereNot(function ($query) use ($relation, $model) {
500-
$query->where($relation->getMorphType(), $model->getMorphClass())
501-
->where($relation->getForeignKeyName(), $model->getKey());
500+
$query->where($relation->getMorphType(), '<=>', $model->getMorphClass())
501+
->where($relation->getForeignKeyName(), '<=>', $model->getKey());
502502
}, null, null, $boolean);
503503
}
504504

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ public function testWhereNotMorphedTo()
16341634

16351635
$builder = $model->whereNotMorphedTo('morph', $relatedModel);
16361636

1637-
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not ("morph_type" = ? and "morph_id" = ?)', $builder->toSql());
1637+
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not ("morph_type" <=> ? and "morph_id" <=> ?)', $builder->toSql());
16381638
$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
16391639
}
16401640

@@ -1662,7 +1662,7 @@ public function testOrWhereNotMorphedTo()
16621662

16631663
$builder = $model->where('bar', 'baz')->orWhereNotMorphedTo('morph', $relatedModel);
16641664

1665-
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not ("morph_type" = ? and "morph_id" = ?)', $builder->toSql());
1665+
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not ("morph_type" <=> ? and "morph_id" <=> ?)', $builder->toSql());
16661666
$this->assertEquals(['baz', $relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
16671667
}
16681668

@@ -1684,7 +1684,7 @@ public function testWhereNotMorphedToClass()
16841684

16851685
$builder = $model->whereNotMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);
16861686

1687-
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not "morph_type" = ?', $builder->toSql());
1687+
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not "morph_type" <=> ?', $builder->toSql());
16881688
$this->assertEquals([EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
16891689
}
16901690

@@ -1706,7 +1706,7 @@ public function testOrWhereNotMorphedToClass()
17061706

17071707
$builder = $model->where('bar', 'baz')->orWhereNotMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);
17081708

1709-
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not "morph_type" = ?', $builder->toSql());
1709+
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not "morph_type" <=> ?', $builder->toSql());
17101710
$this->assertEquals(['baz', EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
17111711
}
17121712

0 commit comments

Comments
 (0)