JCC Eloquent ORM
Observer
Introduction
JCC-Eloquent models can fire lifecycle events whenever records are created, updated, deleted, or restored. You register hooks for those events in two main ways:
static boot()on the model — registerthis.creating(...),this.saved(...), and other callbacks directly on the model class (most common for app-specific logic).- Observer classes — group hooks in a separate class and attach with
@Observer(...)(useful when you want a dedicated, testable listener or constructor injection).
Both approaches use the same event names and run at the same points in the save/delete cycle.
Available events
| Method | When it runs |
|---|---|
saving | Before insert or update |
creating | Before insert (new record) |
updating | Before update (existing record) |
created | After insert |
updated | After update |
saved | After insert or update |
deleting | Before delete |
deleted | After delete |
restoring | Before a soft-deleted row is restored |
restored | After a soft-deleted row is restored |
Typical create order: saving → creating → created → saved.
Typical update order: saving → updating → updated → saved.
Hook callbacks may be async; the framework awaits them during dispatch.
Register hooks on the model (boot())
Override static boot() on your model and call this.creating(...), this.created(...), and the other registrars. The framework calls boot() automatically the first time the model participates in an event (via Event.bootIfNotBooted).
Use this.<event>(callback) inside boot() — the same helpers exist on the model class (User.creating(...)) if you prefer registering outside the class (for example in a service provider).
Optional static booted() runs after boot() if you need a second setup phase.
Observer classes (alternative)
For larger apps, move hooks into a dedicated observer class and register it with @Observer on the model.
Generate an observer
This creates app/Observers/UserObserver.ts with stub methods for every lifecycle hook. When you pass model=User, the scaffold imports your User model and types the method parameters.
Define an observer
Only implement the methods you need — empty stubs can be removed.
The observer is resolved from the service container (app.make(...)), so you can use constructor injection if the class is registered in a provider.
Register on a model
Attach the observer with the @Observer decorator on your model class:
Registration runs when the model class is loaded. The framework wires each implemented observer method to the matching lifecycle event.
Example: password hashing and cache busting
Register outside boot() (alternative)
You can attach the same callbacks without overriding boot():
Prefer static boot() on the model when the hooks belong to that model; use a service provider when wiring events from elsewhere.
Soft deletes
When protected softDeletes = true on the model, restore operations fire restoring and restored. See Soft deletes.
Disable events for specific operations
Skip observers and other model events when you need a silent write (seeders, imports, admin fixes):
See Retrieving models for more detail.
Summary
- Lifecycle events:
saving,creating,updating,created,updated,saved,deleting,deleted,restoring,restored. - Register on the model with
static boot()andthis.creating(...)/this.saved(...)/ etc. - Or use
@Observer(YourObserver)andmake:observerfor a separate listener class. - Use
saveQuietly(),executeWithoutEvents(), and related helpers to skip hooks when needed.