Skip to content

Commit

Permalink
[8.x] Adds a simple where helper for querying relations (laravel#38499
Browse files Browse the repository at this point in the history
)

* Adds simple `where` helper for relations.

* Reordered function values and type hint.

* Added helpers for morph relations.
  • Loading branch information
DarkGhostHunter authored and victorvilella committed Oct 12, 2021
1 parent 0f32d65 commit 3c1663c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,72 @@ public function orWhereDoesntHaveMorph($relation, $types, Closure $callback = nu
return $this->doesntHaveMorph($relation, $types, 'or', $callback);
}

/**
* Add a basic where clause to a relationship query.
*
* @param string $relation
* @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function whereRelation($relation, $column, $operator = null, $value = null)
{
return $this->whereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
* Add an "or where" clause to a relationship query.
*
* @param string $relation
* @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function orWhereRelation($relation, $column, $operator = null, $value = null)
{
return $this->orWhereHas($relation, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
* Add a polymorphic relationship condition to the query with a where clause.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation
* @param string|array $types
* @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function whereMorphRelation($relation, $types, $column, $operator = null, $value = null)
{
return $this->whereHasMorph($relation, $types, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
* Add a polymorphic relationship condition to the query with an "or where" clause.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo|string $relation
* @param string|array $types
* @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function orWhereMorphRelation($relation, $types, $column, $operator = null, $value = null)
{
return $this->orWhereHasMorph($relation, $types, function ($query) use ($column, $operator, $value) {
$query->where($column, $operator, $value);
});
}

/**
* Add subselect queries to include an aggregate value for a relationship.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/Database/EloquentWhereHasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ protected function setUp(): void
(new Comment)->commentable()->associate($post)->save();
}

public function testWhereRelation()
{
$users = User::whereRelation('posts', 'public', true)->get();

$this->assertEquals([1], $users->pluck('id')->all());
}

public function testOrWhereRelation()
{
$users = User::whereRelation('posts', 'public', true)->orWhereRelation('posts', 'public', false)->get();

$this->assertEquals([1, 2], $users->pluck('id')->all());
}

public function testWhereMorphRelation()
{
$comments = Comment::whereMorphRelation('commentable', '*', 'public', true)->get();

$this->assertEquals([1], $comments->pluck('id')->all());
}

public function testOrWhereMorphRelation()
{
$comments = Comment::whereMorphRelation('commentable', '*', 'public', true)
->orWhereMorphRelation('commentable', '*', 'public', false)
->get();

$this->assertEquals([1, 2], $comments->pluck('id')->all());
}

public function testWithCount()
{
$users = User::whereHas('posts', function ($query) {
Expand Down

0 comments on commit 3c1663c

Please sign in to comment.