Advanced Eloquent Custom Casts and Value Objects | 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)    Advanced Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms        On this page       1. [  Why Built-in Casts Fall Short ](#why-built-in-casts-fall-short)
2. [  Anatomy of a Custom Cast ](#anatomy-of-a-custom-cast)
3. [  Wiring It Up ](#wiring-it-up)
4. [  Parameterised Casts ](#parameterised-casts)
5. [  Inbound-Only Casts ](#inbound-only-casts)
6. [  Casting to Enums vs. Custom Cast Classes ](#casting-to-enums-vs-custom-cast-classes)
7. [  Testing Casts in Isolation ](#testing-casts-in-isolation)
8. [  Key Takeaways ](#key-takeaways)

  ![Advanced Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms](https://cdn.msaied.com/367/9c57cfb2f2843dc64aaa6c97bad628af.png)

  #laravel   #eloquent   #value-objects   #casts   #php  

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

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

       Table of contents

1. [  01   Why Built-in Casts Fall Short  ](#why-built-in-casts-fall-short)
2. [  02   Anatomy of a Custom Cast  ](#anatomy-of-a-custom-cast)
3. [  03   Wiring It Up  ](#wiring-it-up)
4. [  04   Parameterised Casts  ](#parameterised-casts)
5. [  05   Inbound-Only Casts  ](#inbound-only-casts)
6. [  06   Casting to Enums vs. Custom Cast Classes  ](#casting-to-enums-vs-custom-cast-classes)
7. [  07   Testing Casts in Isolation  ](#testing-casts-in-isolation)
8. [  08   Key Takeaways  ](#key-takeaways)

 Why Built-in Casts Fall Short
-----------------------------

Laravel ships with a solid set of primitive casts — `integer`, `boolean`, `array`, `encrypted`, `AsCollection`, and so on. They cover 80% of everyday work. The remaining 20% is where models start accumulating `getXAttribute` / `setXAttribute` pairs, helper methods, and subtle inconsistencies between what goes into the database and what comes out.

Custom cast classes solve this cleanly. They are first-class objects, testable in isolation, and composable across multiple models.

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

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

```php
