JCC Express

Scopes

Introduction

JCC-Eloquent supports:

  • local scopes using scopeX methods on a model
  • global scopes using the ScopedBy([...]) decorator

Both are applied through the query builder used by Model.query().


Local scopes

Define local scopes as static methods prefixed with scope.

TypeScript

Use them fluently without the scope prefix:

TypeScript

Global scopes with ScopedBy

Create a scope class that implements Scope.

TypeScript

Attach it to your model:

TypeScript

You can also pass a function scope:

TypeScript

How scope resolution works

  • Global scopes are read from model metadata via getGlobalScopes()
  • Builder applies global scopes when setModel(...) runs
  • Local scopes are resolved dynamically as scope${StudlyName} methods

Scope removal

withoutGlobalScopes(...) exists on the builder, but currently acts as a placeholder and does not remove applied scopes.

Use global scopes with that current behavior in mind.