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.6] Fix MorphTo eager loading and withoutGlobalScopes() #25331

Merged
merged 1 commit into from Aug 26, 2018
Merged

[5.6] Fix MorphTo eager loading and withoutGlobalScopes() #25331

merged 1 commit into from Aug 26, 2018

Conversation

staudenmeir
Copy link
Contributor

withoutGlobalScopes() doesn't work when eager loading MorphTo relationships:

class Comment extends Model {
    public function commentable() {
        return $this->morphTo()->withoutGlobalScopes();
    }
}

class Post extends Model {
    use SoftDeletes;
}

Comment::with('commentable')->get();
# expected
select * from "posts" where "posts"."id" in (?, ?)

# actual
select * from "posts" where "posts"."id" in (?, ?) and "posts"."deleted_at" is null

MorphTo uses a temporary query (based on Comment) to build the relationship. At this point, withoutGlobalScopes() can only remove scopes that were registered on Comment. When getResultsByType() finally executes individual queries for the different related models, the global scopes get re-applied.

We can fix it by catching the withoutGlobalScopes() calls and using the $macroBuffer to apply them later to the individual queries.

Fixes #18052 and #21856.

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.

None yet

2 participants