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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners