pooreal's avatar

Local scopes not working for some models

This is really strange:

in my User.php I have (sample scope):

public static function scopeFirstUser($query) {
      return $query->where('id', 1);
}

I tried with/without static; the result is the same.

When I try \App\User::firstUser()->get() I get this error:

(1/1) BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::firstUser()

But when I copy the scope function to another model like Order.php, \App\Order::firstUser()->get()works fine!!!

0 likes
4 replies
IgorBabko's avatar

Hello -

Local scope methods shouldn't be static:

public function scopeFirstUser($query)
{
      return $query->where('id', 1);
}
pooreal's avatar

@IgorBabko Thanks for your reply. tested both but the result is the same. The strange thing is that this very method works in another model!

ibrahim.auce@gmail.com's avatar

same problem, but when preceded the scope with query it works, so try

\App\User::query()->firstUser()->get()

1 like

Please or to participate in this conversation.