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 relationships with global scope columns #25368

Merged
merged 1 commit into from
Aug 29, 2018
Merged

[5.6] Fix relationships with global scope columns #25368

merged 1 commit into from
Aug 29, 2018

Conversation

staudenmeir
Copy link
Contributor

BelongsToMany relationships don't respect global scopes that limit the selected columns:

class Role extends Model
{
    public static function boot()
    {
        parent::boot();

        static::addGlobalScope(function ($query) {
            $query->select('roles.id');
        });
    }
}

class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

User::first()->roles;
# expected
select "roles"."id", "role_user"."user_id" as "pivot_user_id", [...]

# actual
select "roles"."id", "roles".*, "role_user"."user_id" as "pivot_user_id", [...]

This issue also affects HasManyThrough relationships.

Fixes #22482.

@taylorotwell taylorotwell merged commit a784efc into laravel:5.6 Aug 29, 2018
@KKSzymanowski
Copy link
Contributor

Thank you!

@staudenmeir staudenmeir deleted the scope-columns branch August 29, 2018 13:43
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.

3 participants