Skip to main content

What is a Model?

Think of a model as a blueprint for your data. If you have a “users” table in your database, you create a “User” model to work with user data in your JavaScript code.
Real-world analogy: A model is like a form at the doctor’s office. The form has specific fields (name, age, symptoms), and each filled-out form represents one patient record. The model defines what fields exist, and each instance represents one record.

Creating Your First Model

Manual Creation

Model Configuration

Table Settings

UUID Primary Keys

Set keyType = 'uuid' and incrementing = false — IlanaORM generates a UUID automatically on create:

ULID Primary Keys

ULIDs are 26-character sortable IDs — they’re URL-safe, lexicographically ordered, and include a millisecond timestamp prefix so they sort naturally by creation time:
Use char(26) for the column type in migrations:

Timestamps

Control automatic timestamp handling:

Soft Deletes

Mark records as deleted without actually removing them:

Mass Assignment Protection

Control which fields can be set when creating or updating records:
Security Note: Always use fillable or guarded to prevent users from setting sensitive fields like is_admin or balance.

Attribute Casting

Automatically convert database values to JavaScript types:

Available Cast Types

Cast Class Instances

You can also pass cast class instances (objects with get(value) / set(value) methods). They are called automatically the same way string casts are:
See the Custom Casting guide to build your own cast classes.

Default Values

Set default values for new model instances:

Hidden Attributes

Hide sensitive fields from JSON output:

Mutators and Accessors

Transform data when setting or getting attributes:

Mutators (Setters)

Mutators are called automatically whenever an attribute is set — on direct assignment (user.email = x), via fill(), and via update(). They must return the transformed value.

Accessors (Getters)

Accessors are called automatically on direct property access. List the key in appends to include it in toJSON() output.

Model Events

Hook into the model lifecycle:
Available Events:
  • saving / saved - Before/after create or update
  • creating / created - Before/after create
  • updating / updated - Before/after update
  • deleting / deleted - Before/after delete
  • restoring / restored - Before/after soft delete restore

Muting Events

Sometimes you need to save records without triggering events — for example in seeders, migrations, or bulk imports. Use withoutEvents():

Query Scopes

Create reusable query constraints:

Working with Model Instances

Creating Records

Reading Records

Updating Records

Deleting Records

Pruning Models

Pruning lets you automatically delete stale records (e.g. old logs, expired tokens) by defining what “prunable” means on the model:
Run prune() on a schedule (e.g. a daily cron job) to keep your tables clean.

Model Utilities

Reload and Compare Instances

Replicating Models

replicate() creates an unsaved copy of a model, excluding the primary key and timestamps so it can be inserted as a new row:
You can exclude additional columns from the copy:

Table Helpers

seed() requires a factory to be defined for the model with defineFactory. See Factories for setup details.

Enum Helpers

Define the possible values for an enum column once, and IlanaORM generates isX() / makeX() helpers automatically on every instance:

Check Model State

Convert to Different Formats

Best Practices

1. Use Descriptive Names

2. Always Use Mass Assignment Protection

3. Hide Sensitive Data

4. Use Appropriate Casting

Next Steps

Learn Queries

Discover how to find and filter your data

Set Up Relationships

Connect different models together