Skip to content

Commit

Permalink
[9.x] Make whereBelongsTo accept Collection (#41733)
Browse files Browse the repository at this point in the history
* Add whereBelongsToOneOf clause

* Refactor into whereBelongsTo method

* throw exception on empty

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
erikgaal and taylorotwell authored Mar 30, 2022
1 parent 1a81bc7 commit 5694e1d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
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

0 comments on commit 5694e1d

Please sign in to comment.