Skip to content

Conversation

@Muffinman
Copy link
Contributor

This PR updates the global ScopedBy attribute to match the behaviour of global scopes added via the booted static method.

Example

Global Scope with trait attribute (does not work):

class PublishedScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
      $builder->where('published', '=', 1);
    }
}

#[ScopedBy([PublishedScope::class])]
trait IsPost {

}

// Will not apply the global scope
class Post extends Model {
   use IsPost;
}

Global scope with static booted method (works):

class PublishedScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
      $builder->where('published', '=', 1);
    }
}

trait IsPost {
    protected static function booted(): void
    {
        static::addGlobalScope(new PublishedScope);
    }
}

// Applies global scope
class Post extends Model {
   use IsPost;
}

After this PR both should work and be handled identically.

Targeting 13.x because I think this may be considered a breaking change. I will also leave in draft while I add a test.

@github-actions
Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@Muffinman Muffinman marked this pull request as ready for review September 30, 2025 11:13
@taylorotwell taylorotwell merged commit 26676d8 into laravel:12.x Sep 30, 2025
63 checks passed
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