Skip to content

Commit

Permalink
[10.x] whereMorphedTo null (laravel#46821)
Browse files Browse the repository at this point in the history
* Update QueriesRelationships.php

* Update DatabaseEloquentBuilderTest.php

* Update DatabaseEloquentBuilderTest.php

* Update DatabaseEloquentBuilderTest.php
  • Loading branch information
m-derakhshi authored Apr 18, 2023
1 parent 7aab67d commit 95ffddc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public function orWhereMorphRelation($relation, $types, $column, $operator = nul
* Add a morph-to relationship condition to the query.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation
* @param \Illuminate\Database\Eloquent\Model|string $model
* @param \Illuminate\Database\Eloquent\Model|string|null $model
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function whereMorphedTo($relation, $model, $boolean = 'and')
Expand All @@ -457,6 +457,10 @@ public function whereMorphedTo($relation, $model, $boolean = 'and')
$relation = $this->getRelationWithoutConstraints($relation);
}

if (is_null($model)) {
return $this->whereNull($relation->getMorphType(), $boolean);
}

if (is_string($model)) {
$morphMap = Relation::morphMap();

Expand Down Expand Up @@ -506,7 +510,7 @@ public function whereNotMorphedTo($relation, $model, $boolean = 'and')
* Add a morph-to relationship condition to the query with an "or where" clause.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation
* @param \Illuminate\Database\Eloquent\Model|string $model
* @param \Illuminate\Database\Eloquent\Model|string|null $model
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function orWhereMorphedTo($relation, $model)
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,15 @@ public function testWhereMorphedTo()
$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

public function testWhereMorphedToNull()
{
$model = new EloquentBuilderTestModelParentStub;
$this->mockConnectionForModel($model, '');

$builder = $model->whereMorphedTo('morph', null);
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "morph_type" is null', $builder->toSql());
}

public function testWhereNotMorphedTo()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down Expand Up @@ -1688,6 +1697,17 @@ public function testOrWhereMorphedTo()
$this->assertEquals(['baz', $relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

public function testOrWhereMorphedToNull()
{
$model = new EloquentBuilderTestModelParentStub;
$this->mockConnectionForModel($model, '');

$builder = $model->where('bar', 'baz')->orWhereMorphedTo('morph', null);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or "morph_type" is null', $builder->toSql());
$this->assertEquals(['baz'], $builder->getBindings());
}

public function testOrWhereNotMorphedTo()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down

0 comments on commit 95ffddc

Please sign in to comment.