Mail: [email protected] Phone: +1(424)231-4091

Crud: Scope Classes

While building CRUD components, you might have classes that are based on the same table. Therefore, you want to apply a custom scope to each component to filter how entries are retrieved.

A good example is a blog with PostCrud and MyPostCrud. Both classes use the same table, but MyPostCrud should display the posts created by the logged user. That's where Scope Classes comes in.

Declaring Scope

In your module, scope must be stored on the "Scopes" directory. While this is not a requirement, we highly encourage this approach. The definition of a scope follows the same structure as regular Laravel scope. Here is an example of how scopes are declared:

Using Scopes On Classes

Now that we've defined our scope, we can use it on any Crud class we would like to apply it. Here is how to proceed: