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

Conversation

Projects
None yet
2 participants
@staudenmeir
Contributor

staudenmeir commented Nov 30, 2018

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.

@taylorotwell taylorotwell merged commit d0cf02d into laravel:5.7 Nov 30, 2018

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@staudenmeir staudenmeir deleted the staudenmeir:has-many-through branch Nov 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment