Laravel Cast Helper

Browse built-in cast types and generate custom CastsAttributes classes with get/set transformations.

  1. Home
  2. > Web Dev > Laravel Tools >
  3. 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
arraySerializes/deserializes JSON as a PHP array'options' => 'array'
jsonSame as array, stores as JSON'metadata' => 'json'
booleanConverts to true/false'is_admin' => 'boolean'
integerConverts to an integer'votes' => 'integer'
floatConverts to a float'price' => 'float'
stringConverts to a string'name' => 'string'
datetimeConverts to a Carbon instance'published_at' => 'datetime'
immutable_datetimeConverts to a CarbonImmutable instance'created_at' => 'immutable_datetime'
timestampConverts to a Unix timestamp'published_at' => 'timestamp'
dateConverts 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'
encryptedAutomatically encrypts/decrypts using APP_KEY'secret' => 'encrypted'
objectDeserializes JSON to a stdClass object'config' => 'object'
collectionDeserializes JSON to an Eloquent Collection'tags' => 'collection'
hashHashes the attribute value (configurable algorithm)'password' => 'hash:sha256'
realConverts 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:

  1. Enter the cast class name — e.g. JsonCast or MoneyCast.
  2. Select cast type — Standard (get & set), Get only, Set only, or Inbound (set only validation).
  3. Write the get() and set() logic — Use $value and $attributes to transform data.
  4. Generate — Click Generate Cast to create the PHP class.
  5. 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.