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] Fixed aggregates (e.g.: withExists) for one of many relationships #37413

Merged
merged 2 commits into from
May 19, 2021
Merged

[8.x] Fixed aggregates (e.g.: withExists) for one of many relationships #37413

merged 2 commits into from
May 19, 2021

Conversation

cbl
Copy link
Contributor

@cbl cbl commented May 19, 2021

This pr fixes aggregates like withExists when used for one of many relationships (#37362).

The problem was that the join clause was passed to the existence query, but not the bindings.

The joins are now added included bindings in the following method:

protected function mergeJoinsTo(Builder $query)
{
$query->getQuery()->joins = $this->query->getQuery()->joins;
$query->addBinding($this->query->getBindings(), 'join');
}

Used here in HasOne:

public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
if ($this->isOneOfMany()) {
$this->mergeJoinsTo($query);
}
return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
}

And MorphOne:

public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
if ($this->isOneOfMany()) {
$this->mergeJoinsTo($query);
}
return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
}

@GrahamCampbell GrahamCampbell changed the title [8.x] fixed aggregates (e.g.: withExists) for one of many relationships [8.x] Fixed aggregates (e.g.: withExists) for one of many relationships May 19, 2021
@taylorotwell taylorotwell merged commit 3baf475 into laravel:8.x May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants