When I wrote about Laravel 12.4, I missed a completely new way to write local scopes in Eloquent models 😅. For as long as I can remember, you can define query scopes within a model by prefixing a public method with scope
:
public function scopePopular(Builder $query): void{ $query->where('votes', '>', 100);} User::popular()->orderBy('created_at', 'DESC')->get();
While Laravel 12 applications will still support the scope*
prefixed methods to define a model scope, the new #[Scope]
attribute can be used, and it keeps your method name as intended:
use Illuminate\Database\Eloquent\Attributes\Scope; #[Scope]protected function popular(Builder $query): void{ $query->where('votes', '>', 100);}
You can learn more about scopes and getting started with Eloquent in the Laravel documentation. This feature was merged in Laravel v12.4 - check out pull request #54450 for discussion and implementation details.