Eloquent Custom Casts &amp; Value Objects in Laravel | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. Eloquent Custom Casts: Encapsulating Value Objects Without the Bloat

 Eloquent Custom Casts: Encapsulating Value Objects Without the Bloat
=====================================================================

 Custom Eloquent casts let you map raw database columns directly to rich value objects. Learn how to build type-safe, testable casts that keep your models clean and your domain logic where it belongs.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 17 Jul 2026 · Updated 17 Jul 2026 · 1 min read

ShareCopy linkCopied

 ![Eloquent Custom Casts: Encapsulating Value Objects Without the Bloat](https://cdn.msaied.com/434/ebfa76586107d613c3d3e65695f16ae4.png) 

  On this page +1. [Why Custom Casts Beat Accessors and Mutators](#why-custom-casts-beat-accessors-and-mutators)
2. [Defining the Value Object](#defining-the-value-object)
3. [Implementing the Cast](#implementing-the-cast)
4. [Registering on the Model](#registering-on-the-model)
5. [Handling JSON Columns with CastsAttributes](#handling-json-columns-with-codecastsattributescode)
6. [Inbound-Only Casts](#inbound-only-casts)
7. [Testing the Cast in Isolation](#testing-the-cast-in-isolation)
8. [Key Takeaways](#key-takeaways)

 Why Custom Casts Beat Accessors and Mutators
--------------------------------------------

Accessors and mutators (`get`/`set` attribute methods) are convenient, but they scatter transformation logic across your model and return plain scalars. A custom cast implements `CastsAttributes` and gives you a dedicated class that is independently testable, reusable across models, and expressive about what the column actually holds.

The canonical use-case: a `money` column stored as an integer (cents) that your application always treats as a `Money` value object.

### Defining the Value Object

```php
