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 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)

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

  #laravel   #eloquent   #domain-driven-design   #value-objects   #testing  

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

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

       Table of contents

1. [  01   Why Custom Casts Beat Accessors and Mutators  ](#why-custom-casts-beat-accessors-and-mutators)
2. [  02   Defining the Value Object  ](#defining-the-value-object)
3. [  03   Implementing the Cast  ](#implementing-the-cast)
4. [  04   Registering on the Model  ](#registering-on-the-model)
5. [  05   Handling JSON Columns with CastsAttributes  ](#handling-json-columns-with-codecastsattributescode)
6. [  06   Inbound-Only Casts  ](#inbound-only-casts)
7. [  07   Testing the Cast in Isolation  ](#testing-the-cast-in-isolation)
8. [  08   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
