Skip to content
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

[9.x] Make whereBelongsTo accept Collection #41733

Merged
merged 3 commits into from
Mar 30, 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
21 changes: 17 additions & 4 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use BadMethodCallException;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;
use InvalidArgumentException;

trait QueriesRelationships
{
Expand Down Expand Up @@ -461,7 +463,7 @@ public function orWhereMorphedTo($relation, $model)
/**
* Add a "belongs to" relationship where clause to the query.
*
* @param \Illuminate\Database\Eloquent\Model $related
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> $related
* @param string|null $relationshipName
* @param string $boolean
* @return $this
Expand All @@ -470,6 +472,18 @@ public function orWhereMorphedTo($relation, $model)
*/
public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and')
{
if (! $related instanceof Collection) {
$relatedCollection = $related->newCollection([$related]);
} else {
$relatedCollection = $related;

$related = $relatedCollection->first();
}

if ($relatedCollection->isEmpty()) {
throw new InvalidArgumentException("Collection given to whereBelongsTo method may not be empty.");
}

if ($relationshipName === null) {
$relationshipName = Str::camel(class_basename($related));
}
Expand All @@ -484,10 +498,9 @@ public function whereBelongsTo($related, $relationshipName = null, $boolean = 'a
throw RelationNotFoundException::make($this->model, $relationshipName, BelongsTo::class);
}

$this->where(
$this->whereIn(
$relationship->getQualifiedForeignKeyName(),
'=',
$related->getAttributeValue($relationship->getOwnerKeyName()),
$relatedCollection->pluck($relationship->getOwnerKeyName())->toArray(),
$boolean,
);

Expand Down
28 changes: 26 additions & 2 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1041,18 +1041,42 @@ public function testWhereBelongsTo()
$builder = $this->getBuilder();
$builder->shouldReceive('from')->with('eloquent_builder_test_where_belongs_to_stubs');
$builder->setModel($related);
$builder->getQuery()->shouldReceive('where')->once()->with('eloquent_builder_test_where_belongs_to_stubs.parent_id', '=', 2, 'and');
$builder->getQuery()->shouldReceive('whereIn')->once()->with('eloquent_builder_test_where_belongs_to_stubs.parent_id', [2], 'and');

$result = $builder->whereBelongsTo($parent);
$this->assertEquals($result, $builder);

$builder = $this->getBuilder();
$builder->shouldReceive('from')->with('eloquent_builder_test_where_belongs_to_stubs');
$builder->setModel($related);
$builder->getQuery()->shouldReceive('where')->once()->with('eloquent_builder_test_where_belongs_to_stubs.parent_id', '=', 2, 'and');
$builder->getQuery()->shouldReceive('whereIn')->once()->with('eloquent_builder_test_where_belongs_to_stubs.parent_id', [2], 'and');

$result = $builder->whereBelongsTo($parent, 'parent');
$this->assertEquals($result, $builder);

$parents = new Collection([new EloquentBuilderTestWhereBelongsToStub([
'id' => 2,
'parent_id' => 1,
]), new EloquentBuilderTestWhereBelongsToStub([
'id' => 3,
'parent_id' => 1,
])]);

$builder = $this->getBuilder();
$builder->shouldReceive('from')->with('eloquent_builder_test_where_belongs_to_stubs');
$builder->setModel($related);
$builder->getQuery()->shouldReceive('whereIn')->once()->with('eloquent_builder_test_where_belongs_to_stubs.parent_id', [2, 3], 'and');

$result = $builder->whereBelongsTo($parents);
$this->assertEquals($result, $builder);

$builder = $this->getBuilder();
$builder->shouldReceive('from')->with('eloquent_builder_test_where_belongs_to_stubs');
$builder->setModel($related);
$builder->getQuery()->shouldReceive('whereIn')->once()->with('eloquent_builder_test_where_belongs_to_stubs.parent_id', [2, 3], 'and');

$result = $builder->whereBelongsTo($parents, 'parent');
$this->assertEquals($result, $builder);
}

public function testDeleteOverride()
Expand Down