DavidAPowers's avatar

Can't get new Scope method to work (Laravel 12.8.1)

For some reason I cannot get the new method of indicating scopes to work, but the old way works just fine. I thought I just followed the documentation example, but I'm getting a missing arguments error as a result of the Builder $query argument. I'm on Laravel 12.8.1. Does anyone know what's going on?

Model:

    #[Scope]
    protected function published(Builder $query): void
    {
        $query->where('published_on', '!=', '');
    }


    #[Scope]
    protected function newestFirst(Builder $query): void
    {
        $query->orderBy('published_on','desc');
    }

Service class:

    public function scopeTest():Collection {
        return Post::published()->newestFirst()->get();
    }

Error:

Too few arguments to function App\Models\Post::published(), 0 passed in D:\repos\aldint\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 2465 and exactly 1 expected
0 likes
3 replies
jj15's avatar

Did you ensure that the #[Scope] attribute was imported?

use Illuminate\Database\Eloquent\Attributes\Scope;
1 like
DavidAPowers's avatar

@jj15 DOH!!! -- I'm an idiot-- it HAD been imported, but apparently I accidentally removed it when I was undoing something else. Thank you so much!

Please or to participate in this conversation.