Skip to main content

What is Custom Casting?

Custom casting allows you to transform data when it’s retrieved from or stored to the database. While IlanaORM provides built-in casts like 'date', 'boolean', and 'json', custom casts let you create your own transformation logic.
Cast classes are called automatically. Any object with get(value) / set(value) methods placed in a model’s casts map will have set() called on write and get() called on read — exactly like string cast types. No manual invocation needed.
Real-world analogy: Custom casting is like having a translator who speaks multiple languages. When data comes from the database (foreign language), the translator converts it to something your application understands (native language). When saving data, the translator converts it back to the database format.

Built-in Casts

Before creating custom casts, let’s review what’s available:

Creating Custom Casts

Using the CLI

Basic Cast Structure

Using Custom Casts

Advanced Cast Examples

Encrypted Cast

The built-in EncryptedCast from ilana-orm/orm/CustomCasts uses base64 encoding only — not real encryption. For production use, implement your own cast with a proper encryption library as shown below.

Address Cast

Phone Number Cast

Color Cast

Parameterized Casts

Cast with Configuration

Configurable JSON Cast

Cast Collections

Testing Custom Casts

Unit Testing Casts

Integration Testing

Performance Considerations

Lazy Cast Evaluation

Async Casts (Advanced)

Best Practices

1. Keep Casts Simple

2. Handle Edge Cases

3. Make Casts Reversible

4. Document Cast Behavior

Next Steps

Query Scopes

Create reusable query constraints

API Reference

Explore the complete API documentation