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

[8.x] Adds a simple where helper for querying relations #38499

Merged
merged 3 commits into from
Aug 26, 2021
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
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