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

[5.7] Fix HasManyThrough existence queries with same parent and through parent table #26676

Merged
merged 1 commit into from
Nov 30, 2018
Merged

[5.7] Fix HasManyThrough existence queries with same parent and through parent table #26676

merged 1 commit into from
Nov 30, 2018

Conversation

staudenmeir
Copy link
Contributor

Laravel already supports self-referencing HasManyThrough relationships with the same parent and related model (#26662).

This PR adds support for relationships with the same parent and through parent model:

class Category extends Model {
    public function subProducts() {
        return $this->hasManyThrough(Product::class, self::class, 'parent_id');
    }
}

Category::has('subProducts')->get();

In existence/withCount() queries, we have to alias the through parent table:

# expected
select * from `categories` where exists (
  select * from `products`
  inner join `categories` as `laravel_reserved_0`
  on `laravel_reserved_0`.`id` = `products`.`category_id`
  where `categories`.`id` = `laravel_reserved_0`.`parent_id`
)

# actual
select * from `categories` where exists (
  select * from `products`
  inner join `categories`
  on `categories`.`id` = `products`.`category_id`
  where `categories`.`id` = `categories`.`parent_id`
)

Fixes #26623.

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