What are custom Eloquent cast types in Laravel?

Answer

Custom cast types allow you to define reusable type transformations for Eloquent model attributes. Implement CastsAttributes: class MoneyCast implements CastsAttributes { public function get($model, $key, $value, $attributes) { return new Money($value, $attributes["currency"]); } public function set($model, $key, $value, $attributes) { return ["amount" => $value->getAmount(), "currency" => $value->getCurrency()]; } }. Register in model: protected $casts = ["price" => MoneyCast::class]. Inbound-only casts (transform only on set) implement CastsInboundAttributes. Castable classes implement Castable with a castUsing() method that returns the cast class name. Use cases: Money value objects, encrypted fields, custom date formats, geographic coordinates. Custom casts make the model the single source of truth for data transformations, keeping controllers and services clean.