Skip to content

Commit

Permalink
Refactor into whereBelongsTo method
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Mar 30, 2022
1 parent 605ec9e commit 4268af6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 65 deletions.
69 changes: 14 additions & 55 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -461,7 +462,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,57 +471,14 @@ public function orWhereMorphedTo($relation, $model)
*/
public function whereBelongsTo($related, $relationshipName = null, $boolean = 'and')
{
if ($relationshipName === null) {
$relationshipName = Str::camel(class_basename($related));
}

try {
$relationship = $this->model->{$relationshipName}();
} catch (BadMethodCallException $exception) {
throw RelationNotFoundException::make($this->model, $relationshipName);
}

if (! $relationship instanceof BelongsTo) {
throw RelationNotFoundException::make($this->model, $relationshipName, BelongsTo::class);
if (! $related instanceof Collection) {
$values = $related->newCollection([$related]);
} else {
$values = $related;
$related = $values->first();
}

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

return $this;
}

/**
* Add an "BelongsTo" relationship with an "or where" clause to the query.
*
* @param \Illuminate\Database\Eloquent\Model $related
* @param string|null $relationshipName
* @return $this
*
* @throws \RuntimeException
*/
public function orWhereBelongsTo($related, $relationshipName = null)
{
return $this->whereBelongsTo($related, $relationshipName, 'or');
}

/**
* Add a "belongs to one of" relationship where clause to the query.
*
* @param \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> $values
* @param string|null $relationshipName
* @param string $boolean
* @return $this
*
* @throws \Illuminate\Database\Eloquent\RelationNotFoundException
*/
public function whereBelongsToOneOf($values, $relationshipName = null, $boolean = 'and')
{
if (! $related = $values->first()) {
if (! $related) {
return $this->whereNull($this->getModel()->getKeyName(), $boolean);
}

Expand All @@ -540,6 +498,7 @@ public function whereBelongsToOneOf($values, $relationshipName = null, $boolean

$this->whereIn(
$relationship->getQualifiedForeignKeyName(),
'=',
$values->pluck($relationship->getOwnerKeyName())->toArray(),
$boolean,
);
Expand All @@ -548,17 +507,17 @@ public function whereBelongsToOneOf($values, $relationshipName = null, $boolean
}

/**
* Add a "belongs to one of" relationship with an or where clause to the query.
* Add an "BelongsTo" relationship with an "or where" clause to the query.
*
* @param \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model> $values
* @param \Illuminate\Database\Eloquent\Model $related
* @param string|null $relationshipName
* @return $this
*
* @throws \Illuminate\Database\Eloquent\RelationNotFoundException
* @throws \RuntimeException
*/
public function orWhereBelongsToOneOf($values, string $relationshipName = null)
public function orWhereBelongsTo($related, $relationshipName = null)
{
return $this->whereBelongsToOneOf($values, $relationshipName, 'or');
return $this->whereBelongsTo($related, $relationshipName, 'or');
}

/**
Expand Down
12 changes: 2 additions & 10 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1041,26 +1041,18 @@ 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);
}

public function testWhereBelongsToOneOf()
{
$related = new EloquentBuilderTestWhereBelongsToStub([
'id' => 1,
'parent_id' => 2,
]);

$parents = new Collection([new EloquentBuilderTestWhereBelongsToStub([
'id' => 2,
Expand Down

0 comments on commit 4268af6

Please sign in to comment.