Skip to content

[9.x] Add whereNotMorphedTo, orWhereNotMorphedTo #42264

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 1 commit into from
May 5, 2022
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
41 changes: 41 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,35 @@ public function whereMorphedTo($relation, $model, $boolean = 'and')
}, null, null, $boolean);
}

/**
* Add a not morph-to relationship condition to the query.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation
* @param \Illuminate\Database\Eloquent\Model|string $model
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function whereNotMorphedTo($relation, $model, $boolean = 'and')
{
if (is_string($relation)) {
$relation = $this->getRelationWithoutConstraints($relation);
}

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

if (! empty($morphMap) && in_array($model, $morphMap)) {
$model = array_search($model, $morphMap, true);
}

return $this->whereNot($relation->getMorphType(), $model, null, $boolean);
}

return $this->whereNot(function ($query) use ($relation, $model) {
$query->where($relation->getMorphType(), $model->getMorphClass())
->where($relation->getForeignKeyName(), $model->getKey());
}, null, null, $boolean);
}

/**
* Add a morph-to relationship condition to the query with an "or where" clause.
*
Expand All @@ -460,6 +489,18 @@ public function orWhereMorphedTo($relation, $model)
return $this->whereMorphedTo($relation, $model, 'or');
}

/**
* Add a not 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
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function orWhereNotMorphedTo($relation, $model)
{
return $this->whereNotMorphedTo($relation, $model, 'or');
}

/**
* Add a "belongs to" relationship where clause to the query.
*
Expand Down
61 changes: 61 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,20 @@ public function testWhereMorphedTo()
$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

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

$relatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$relatedModel->id = 1;

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

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

public function testOrWhereMorphedTo()
{
$model = new EloquentBuilderTestModelParentStub;
Expand All @@ -1623,6 +1637,20 @@ public function testOrWhereMorphedTo()
$this->assertEquals(['baz', $relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

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

$relatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$relatedModel->id = 1;

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

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

public function testWhereMorphedToClass()
{
$model = new EloquentBuilderTestModelParentStub;
Expand All @@ -1634,6 +1662,39 @@ public function testWhereMorphedToClass()
$this->assertEquals([EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
}

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

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

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

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

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

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

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

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

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

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