Part A: ( Introduction )
Local scopes allow you to define common sets of query constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered “popular”. To define a scope, prefix an Eloquent model method with
scope
.
Scopes should always return a query builder instance.
Here you see a Laravel Model Scope
The usage will be:
Part B: Static Call to Laravel Scope
Instead of putting get()
method when retrieving those specific kind of users like above, we can put get()
in one of the definition of Laravel scope like below which complete the query.
And then use like this without ending get()
;
or if you don’t want the popular users and need only active users you can use like this:
Which seems to be a normal static call but to the Laravel eloquent query scope!
I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.
https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs