Laravel Cast Helper
Browse built-in cast types and generate custom CastsAttributes classes with get/set transformations.
- Home
- > Web Dev > Laravel Tools >
- Cast Helper
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.
Generated Cast
Built-in Cast Types Reference
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' |
How to Use the Cast Helper
This tool helps you create custom Eloquent cast classes. Follow these steps:
- Enter the cast class name — e.g.
JsonCastorMoneyCast. - Select cast type — Standard (get & set), Get only, Set only, or Inbound (set only validation).
- Write the get() and set() logic — Use
$valueand$attributesto transform data. - Generate — Click Generate Cast to create the PHP class.
- Use in your model — Add
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.
Frequently Asked Questions
What is the difference between CastsAttributes and CastsInboundAttributes?
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.
Can my custom cast accept parameters?
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.
How do I register a custom cast on a model?
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'.
When should I use a custom cast vs. an accessor/mutator?
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.