AES Encryption Explained: How It Works and Why It Matters
Jun 23, 2026
Browse built-in cast types and generate custom CastsAttributes classes with get/set transformations.
CamelCase, e.g. JsonCast, MoneyCast, EncryptedCast
Brief description for the class DocBlock.
Add custom constructor parameters for configurable casts.
PHP code for the body of the get() method. Available: $model, $key, $value, $attributes.
PHP code for the body of the set() method. Available: $model, $key, $value, $attributes.
Laravel provides these built-in cast types for Eloquent model attributes. Use them in the protected $casts property.
| Cast Type | Description | Example |
|---|---|---|
| array | Serializes/deserializes JSON as a PHP array | 'options' => 'array' |
| json | Same as array, stores as JSON | 'metadata' => 'json' |
| boolean | Converts to true/false | 'is_admin' => 'boolean' |
| integer | Converts to an integer | 'votes' => 'integer' |
| float | Converts to a float | 'price' => 'float' |
| string | Converts to a string | 'name' => 'string' |
| datetime | Converts to a Carbon instance | 'published_at' => 'datetime' |
| immutable_datetime | Converts to a CarbonImmutable instance | 'created_at' => 'immutable_datetime' |
| timestamp | Converts to a Unix timestamp | 'published_at' => 'timestamp' |
| date | Converts to a Carbon date (no time component) | 'birthday' => 'date:Y-m-d' |
| decimal:{precision} | Converts to a float with fixed decimal places | 'price' => 'decimal:2' |
| encrypted | Automatically encrypts/decrypts using APP_KEY | 'secret' => 'encrypted' |
| object | Deserializes JSON to a stdClass object | 'config' => 'object' |
| collection | Deserializes JSON to an Eloquent Collection | 'tags' => 'collection' |
| hash | Hashes the attribute value (configurable algorithm) | 'password' => 'hash:sha256' |
| real | Converts to a float (alias for float) | 'score' => 'real' |
This tool helps you create custom Eloquent cast classes. Follow these steps:
JsonCast or MoneyCast.$value and $attributes to transform data.protected $casts = ['column' => JsonCast::class]Tip
Place custom cast classes in app/Casts/ and register them in your model using the CastsAttributes or CastsInboundAttributes contract.
CastsAttributes transforms values on both get (DB → PHP) and set (PHP → DB). CastsInboundAttributes only transforms on set — useful for validating or normalizing data before it is stored, without modifying the retrieved value.
Yes. Add a constructor with parameters. When registering the cast, pass parameters like a comma-separated string: 'column' => 'MyCast:param1,param2' or use the cast:param syntax.
Add the cast class to the model's $casts array: protected $casts = ['json_column' => JsonCast::class]. You can also pass parameters: 'column' => 'App\Casts\MoneyCast:USD'.
Custom casts implement a contract (CastsAttributes) and are reusable across models. Accessors/mutators are defined per-model. Use custom casts when you need the same transformation logic in multiple models, or for complex value objects.
Blog
Jun 23, 2026
Jun 23, 2026
Jun 23, 2026
Jun 23, 2026
Jun 23, 2026
Jun 23, 2026