Eloquent Custom Casts &amp; Value Objects in Laravel | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Eloquent Custom Casts: Encapsulating Value Objects Without the Bloat        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)

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

  #laravel   #eloquent   #ddd   #value-objects   #casts  

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

     14 Jun 2026      1 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   Why Custom Casts Belong in Your Toolkit  ](#why-custom-casts-belong-in-your-toolkit)
2. [  02   Building a Money Value Object Cast  ](#building-a-codemoneycode-value-object-cast)
3. [  03   Parameterised Casts  ](#parameterised-casts)
4. [  04   Testing the Cast in Isolation  ](#testing-the-cast-in-isolation)
5. [  05   Pitfalls to Avoid  ](#pitfalls-to-avoid)
6. [  06   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
