Skip to main content

Storing JSON

Add a JSON column in your migration:

Auto-Casting JSON

Use casts to automatically parse/serialize JSON on read and write:
Now you work with native JS objects/arrays — no JSON.parse() needed:

Querying JSON Columns

Check if an Array Contains a Value

Check Array Length

whereJsonContains and whereJsonLength work across MySQL, PostgreSQL, and SQLite. The correct SQL function is chosen automatically based on your database driver.

Updating JSON Fields

Replace the entire field:

Using a Custom Cast Class

For complex transformations, write a cast class:

Real-World Example: User Preferences