Custom Eloquent 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. Contextual Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms

 Contextual Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms
===========================================================================================

 Go beyond primitive casts. Learn how to build custom Eloquent cast classes that hydrate value objects, handle inbound-only transforms, and stay testable without bloating your models.

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

ShareCopy linkCopied

 ![Contextual Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms](https://cdn.msaied.com/552/a7825c0c6f53d934f84fce522573eafb.png) 

  On this page +1. [Why Built-in Casts Stop Being Enough](#why-built-in-casts-stop-being-enough)
2. [Anatomy of a Custom Cast Class](#anatomy-of-a-custom-cast-class)
3. [Inbound-Only Casts](#inbound-only-casts)
4. [Hydrating Composite Value Objects](#hydrating-composite-value-objects)
5. [Testing Cast Classes in Isolation](#testing-cast-classes-in-isolation)
6. [Takeaways](#takeaways)

 Why Built-in Casts Stop Being Enough
------------------------------------

Laravel ships with a solid set of primitive casts — `integer`, `boolean`, `encrypted`, `AsCollection`, and friends. They cover 80% of daily work. The remaining 20% is where bugs hide: money stored as integers but displayed as decimals, coordinates serialised as JSON but consumed as typed objects, or phone numbers that must be normalised on write and formatted on read.

Custom cast classes solve all three cases without polluting model methods or observers.

---

Anatomy of a Custom Cast Class
------------------------------

A cast class implements `CastsAttributes`. The two methods are `get` (hydrate from DB) and `set` (dehydrate to DB).

```php
