Skip to content

[11.x] Pass collection of models to whereMorphedTo / whereNotMorphedTo #54324

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
40 changes: 30 additions & 10 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public function orWhereMorphDoesntHaveRelation($relation, $types, $column, $oper
* Add a morph-to relationship condition to the query.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param \Illuminate\Database\Eloquent\Model|string|null $model
* @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string|null $model
* @return $this
*/
public function whereMorphedTo($relation, $model, $boolean = 'and')
Expand All @@ -559,17 +559,27 @@ public function whereMorphedTo($relation, $model, $boolean = 'and')
return $this->where($relation->qualifyColumn($relation->getMorphType()), $model, null, $boolean);
}

return $this->where(function ($query) use ($relation, $model) {
$query->where($relation->qualifyColumn($relation->getMorphType()), $model->getMorphClass())
->where($relation->qualifyColumn($relation->getForeignKeyName()), $model->getKey());
$models = BaseCollection::wrap($model);

if ($models->isEmpty()) {
throw new InvalidArgumentException('Collection given to whereMorphedTo method may not be empty.');
}

return $this->where(function ($query) use ($relation, $models) {
$models->groupBy(fn ($model) => $model->getMorphClass())->each(function ($models) use ($query, $relation) {
$query->orWhere(function ($query) use ($relation, $models) {
$query->where($relation->qualifyColumn($relation->getMorphType()), $models->first()->getMorphClass())
->whereIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey());
});
});
}, 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
* @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string $model
* @return $this
*/
public function whereNotMorphedTo($relation, $model, $boolean = 'and')
Expand All @@ -588,17 +598,27 @@ public function whereNotMorphedTo($relation, $model, $boolean = 'and')
return $this->whereNot($relation->qualifyColumn($relation->getMorphType()), '<=>', $model, $boolean);
}

return $this->whereNot(function ($query) use ($relation, $model) {
$query->where($relation->qualifyColumn($relation->getMorphType()), '<=>', $model->getMorphClass())
->where($relation->qualifyColumn($relation->getForeignKeyName()), '<=>', $model->getKey());
$models = BaseCollection::wrap($model);

if ($models->isEmpty()) {
throw new InvalidArgumentException('Collection given to whereNotMorphedTo method may not be empty.');
}

return $this->whereNot(function ($query) use ($relation, $models) {
$models->groupBy(fn ($model) => $model->getMorphClass())->each(function ($models) use ($query, $relation) {
$query->orWhere(function ($query) use ($relation, $models) {
$query->where($relation->qualifyColumn($relation->getMorphType()), '<=>', $models->first()->getMorphClass())
->whereNotIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey());
Copy link
Contributor

@owenvoke owenvoke Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdebrauwer, should this be changed to whereIn() as it's negating a negative at the moment? 🤔 We are experiencing issues with whereNotMorphedTo() on upgrading to 11.40.x

Here's a comparison of the differences:

# Previous logic (`<11.40`)
select *
from `posts`
where not (`posts`.`author_type` <=> 'user' and `posts`.`author_id` <=> 1);

# Current logic (`>=11.40`)
select *
from `posts`
where not ((`posts`.`author_type` <=> 'user' and `posts`.`author_id` not in (1)));

# Updated logic (with `whereIn()`), this logic more aligns with the previous logic
select *
from `posts`
where not ((`posts`.`author_type` <=> 'user' and `posts`.`author_id` in (1)));

I've opened a PR here: #54902

});
});
}, null, null, $boolean);
}

/**
* 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|null $model
* @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string|null $model
* @return $this
*/
public function orWhereMorphedTo($relation, $model)
Expand All @@ -610,7 +630,7 @@ public function orWhereMorphedTo($relation, $model)
* 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
* @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string $model
* @return $this
*/
public function orWhereNotMorphedTo($relation, $model)
Expand Down
156 changes: 152 additions & 4 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1774,10 +1774,47 @@ public function testWhereMorphedTo()

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

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$secondRelatedModel->id = 2;

$builder = $model->whereMorphedTo('morph', new Collection([$firstRelatedModel, $secondRelatedModel]));

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelFarRelatedStub;
$secondRelatedModel->id = 2;

$thirdRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$thirdRelatedModel->id = 3;

$builder = $model->whereMorphedTo('morph', [$firstRelatedModel, $secondRelatedModel, $thirdRelatedModel]);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where (("eloquent_builder_test_model_parent_stubs"."morph_type" = ? and "eloquent_builder_test_model_parent_stubs"."morph_id" in (?, ?)) or ("eloquent_builder_test_model_parent_stubs"."morph_type" = ? and "eloquent_builder_test_model_parent_stubs"."morph_id" in (?)))', $builder->toSql());
$this->assertEquals([$firstRelatedModel->getMorphClass(), $firstRelatedModel->getKey(), $thirdRelatedModel->getKey(), $secondRelatedModel->getMorphClass(), $secondRelatedModel->id], $builder->getBindings());
}

public function testWhereMorphedToNull()
{
$model = new EloquentBuilderTestModelParentStub;
Expand All @@ -1797,10 +1834,47 @@ public function testWhereNotMorphedTo()

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

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$secondRelatedModel->id = 2;

$builder = $model->whereNotMorphedTo('morph', new Collection([$firstRelatedModel, $secondRelatedModel]));

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelFarRelatedStub;
$secondRelatedModel->id = 2;

$thirdRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$thirdRelatedModel->id = 3;

$builder = $model->whereNotMorphedTo('morph', [$firstRelatedModel, $secondRelatedModel, $thirdRelatedModel]);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not (("eloquent_builder_test_model_parent_stubs"."morph_type" <=> ? and "eloquent_builder_test_model_parent_stubs"."morph_id" not in (?, ?)) or ("eloquent_builder_test_model_parent_stubs"."morph_type" <=> ? and "eloquent_builder_test_model_parent_stubs"."morph_id" not in (?)))', $builder->toSql());
$this->assertEquals([$firstRelatedModel->getMorphClass(), $firstRelatedModel->getKey(), $thirdRelatedModel->getKey(), $secondRelatedModel->getMorphClass(), $secondRelatedModel->id], $builder->getBindings());
}

public function testOrWhereMorphedTo()
{
$model = new EloquentBuilderTestModelParentStub;
Expand All @@ -1811,10 +1885,47 @@ public function testOrWhereMorphedTo()

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

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$secondRelatedModel->id = 2;

$builder = $model->where('bar', 'baz')->orWhereMorphedTo('morph', new Collection([$firstRelatedModel, $secondRelatedModel]));

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelFarRelatedStub;
$secondRelatedModel->id = 2;

$thirdRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$thirdRelatedModel->id = 3;

$builder = $model->where('bar', 'baz')->orWhereMorphedTo('morph', [$firstRelatedModel, $secondRelatedModel, $thirdRelatedModel]);

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

public function testOrWhereMorphedToNull()
{
$model = new EloquentBuilderTestModelParentStub;
Expand All @@ -1836,10 +1947,47 @@ public function testOrWhereNotMorphedTo()

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

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$secondRelatedModel->id = 2;

$builder = $model->where('bar', 'baz')->orWhereNotMorphedTo('morph', new Collection([$firstRelatedModel, $secondRelatedModel]));

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

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

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

$secondRelatedModel = new EloquentBuilderTestModelFarRelatedStub;
$secondRelatedModel->id = 2;

$thirdRelatedModel = new EloquentBuilderTestModelCloseRelatedStub;
$thirdRelatedModel->id = 3;

$builder = $model->where('bar', 'baz')->orWhereNotMorphedTo('morph', [$firstRelatedModel, $secondRelatedModel, $thirdRelatedModel]);

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

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