Skip to content

Attribute Scope - methods can no longer be called statically on the model #55414

Closed
@jackbayliss

Description

@jackbayliss

Laravel Version

12.8.1

PHP Version

8.4.2

Database Driver & Version

No response

Description

Previously, we were able to call scope methods statically (basic example):

public function scopeActive(Builder $query): Builder { 
    return $query->whereNotNull('active');
} 

We could call:

User::active()->get();

If we change this to using the Scope Attribute, this no longer works:

#[Scope]
public function active(Builder $query): Builder { 
    return $query->whereNotNull('active');
} 
User::active()->get(); 
// Non-static method App\Models\User::active() cannot be called statically

The fix for this is using query() so:

User::query()->active()->get(); 

I guess I opened this issue because I found it strange as I was expecting it to work the same, also the documentation suggests it to work fine

I understand why it's doing it, ie because __callStatic isn't actually getting called because the method actually exists, but opened this for visibility / a point of discussion as ideally it would be nice for it to work as it did before 🤷🏻

Steps To Reproduce

  • Create a model
  • Create a method using the scope attribute
  • Try to call the method statically on the model
  • Witness an error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions