Category: Laravel

Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes can be defined either in the model class or in a separate scope class, also a scope can be local, or global, a scope also can be dynamic, i.e receiving parameters when it is been used. The above code block shows an example of a local scope, a scope is just a method in your model class, that starts with the word “scope” and then the scope name, and can be used with your model.

To make queries without the declared global scope, the withoutGlobalScope static method is called with the model and provided the scope name: Local scopes are user applied, used when the query is built while global scopes are applied to all queries on that model. An example of global scope used by Laravel is the retrieving of non-deleted records (if using soft deletes) when querying a model, and to retrieve records with the deleted records, you make use of the local scope withTrashed
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners