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 bind rich value objects directly to model attributes. This article shows how to build clean, testable casts that enforce domain rules at the boundary between your database and PHP.

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

ShareCopy linkCopied

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

  On this page +1. [Why Custom Casts Belong in Your Toolkit](#why-custom-casts-belong-in-your-toolkit)
2. [Building a Money Value Object Cast](#building-a-codemoneycode-value-object-cast)
3. [Parameterised Casts](#parameterised-casts)
4. [Testing the Cast in Isolation](#testing-the-cast-in-isolation)
5. [Pitfalls to Avoid](#pitfalls-to-avoid)
6. [Takeaways](#takeaways)

 Why Custom Casts Belong in Your Toolkit
---------------------------------------

Laravel ships with a solid set of built-in casts — `integer`, `boolean`, `encrypted`, `AsCollection` — but the moment your domain has a concept like `Money`, `EmailAddress`, or `Coordinate`, you're left choosing between anemic strings in your models or a pile of accessor/mutator pairs.

Custom casts via `CastsAttributes` give you a third, cleaner option: a dedicated class that owns the serialisation contract and enforces invariants every time the attribute is read or written.

---

Building a `Money` Value Object Cast
------------------------------------

Start with the value object itself. Keep it immutable and self-validating.

```php
