PHPackages                             morrislaptop/laravel-popo-caster - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. morrislaptop/laravel-popo-caster

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

morrislaptop/laravel-popo-caster
================================

Automatically cast JSON columns to rich PHP objects in Laravel using Symfony's Serializer

v0.14.0(3mo ago)27648.8k↓86.4%4MITPHPPHP ^8.1CI passing

Since Jan 25Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/morrislaptop/laravel-popo-caster)[ Packagist](https://packagist.org/packages/morrislaptop/laravel-popo-caster)[ Docs](https://github.com/morrislaptop/laravel-popo-caster)[ GitHub Sponsors](https://github.com/morrislaptop)[ RSS](/packages/morrislaptop-laravel-popo-caster/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (22)Versions (20)Used By (0)

Automatically cast JSON columns to rich PHP objects in Laravel using Symfony's Serializer
=========================================================================================

[](#automatically-cast-json-columns-to-rich-php-objects-in-laravel-using-symfonys-serializer)

[![Latest Version on Packagist](https://camo.githubusercontent.com/76f1e1c5a7a464645d63c45c7e6e5d7e342592dcd1bf79ce8c5b16f2c068c011/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f727269736c6170746f702f6c61726176656c2d706f706f2d6361737465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/morrislaptop/laravel-popo-caster)[![GitHub Tests Action Status](https://camo.githubusercontent.com/4b5aa7fca56e8e6a05b95a4080fd57838ea95a3d6b2beaf01533b9760d2b2d9e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d6f727269736c6170746f702f6c61726176656c2d706f706f2d6361737465722f54657374733f6c6162656c3d7465737473)](https://github.com/morrislaptop/laravel-popo-caster/actions?query=workflow%3ATests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/c2023a94b4e18e8a0ef5177cd150ed9f038ceda32a64284237625fae03ff6da4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6f727269736c6170746f702f6c61726176656c2d706f706f2d6361737465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/morrislaptop/laravel-popo-caster)

Laravel is awesome. Spatie's [data transfer object](https://github.com/spatie/data-transfer-object) package for PHP is awesome. But they don't cast objects like dates to DateTimes and collections are a bit of pain. Plain Old PHP Objects (POPOs) are a bit better in that regard.

Have you ever wanted to cast your JSON columns to a value object?

This package gives you 2 caster classes:

- `Serializer` which serializes your value object and stores it in a single JSON field
- `Normalizer` which normalizes your value object and stores the properties as fields on your model

Under the hood it implements Laravel's [`Castable` interface](https://laravel.com/docs/12.x/eloquent-mutators#castables) with a Laravel [custom cast](https://laravel.com/docs/12.x/eloquent-mutators#custom-casts) that handles serializing between the `object` (or a compatible array) and your JSON database column. It uses [Symfony's Serializer](https://symfony.com/doc/current/components/serializer.html) to do this.

This package is inspired by [Laravel Castable Data Transfer Object](https://github.com/jessarcher/laravel-castable-data-transfer-object)!

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require morrislaptop/laravel-popo-caster
```

Serializer Usage
----------------

[](#serializer-usage)

### 1. Create your `POPO`

[](#1-create-your-popo)

```
namespace App\Values;

class Address
{
    public function __construct(
        public string $street,
        public string $suburb,
        public string $state,
        public Carbon $moved_at,
    ) {
    }
}
```

### 2. Configure your Eloquent attribute to cast to it:

[](#2-configure-your-eloquent-attribute-to-cast-to-it)

Note that this should be a `jsonb` or `json` column in your database schema. Objects and arrays are both supported.

```
namespace App\Models;

use App\Values\Address;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\LaravelPopoCaster\Serializer;

/**
 * @property Address $address
 */
class User extends Model
{
    protected $casts = [
        'address' => Serializer::class . ':' . Address::class,
        'prev_addresses' => Serializer::class . ':' . Address::class . '[]',
    ];
}
```

And that's it! You can now pass either an instance of your `Address` class, or even just an array with a compatible structure. It will automatically be cast between your class and JSON for storage and the data will be validated on the way in and out.

```
$user = User::create([
    // ...
    'address' => [
        'street' => '1640 Riverside Drive',
        'suburb' => 'Hill Valley',
        'state' => 'California',
        'moved_at' => now(),
    ],
    'addresses' => [
        [
            'street' => '42 Wallaby Way',
            'suburb' => 'Sydney',
            'state' => 'NSW',
            'moved_at' => '2020-01-14T00:00:00Z',
        ],
    ]
])

$residents = User::where('address->suburb', 'Hill Valley')->get();
```

But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.

```
$user->address->toMapUrl();

$user->address->getCoordinates();

$user->address->getPostageCost($sender);

$user->address->calculateDistance($otherUser->address);

$user->address->moved_at->diffForHumans();

echo (string) $user->address;
```

Normalizer Usage
----------------

[](#normalizer-usage)

### 1. Create your `POPO`

[](#1-create-your-popo-1)

```
namespace App\Values;

class Money
{
    public function __construct(
        public int $amount,
        public string $currency,
    ) {
    }
}
```

### 2. Configure your Eloquent attribute to cast to it:

[](#2-configure-your-eloquent-attribute-to-cast-to-it-1)

Note that the properties of your value object should be columns in your database schema.

```
namespace App\Models;

use App\Values\Money;
use Illuminate\Database\Eloquent\Model;
use Morrislaptop\LaravelPopoCaster\Normalizer;

/**
 * @property Money $money
 */
class User extends Model
{
    protected $casts = [
        'money' => Normalizer::class . ':' . Money::class,
    ];
}
```

And that's it! You can now pass either an instance of your `Money` class, or set the individual properties on the model. It will automatically be cast between your class and properties for storage and the data will be validated on the way in and out.

```
$user = User::create([
    // ...
    'amount' => 1000,
    'curency' => 'AUD',
]);

$user = User::create([
    // ...
    'money' => new Money(1000, 'AUD'),
])
```

But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.

```
$user->money->convertTo('USD');
```

Plug
----

[](#plug)

Want an easy way to mock or have factories for your POPOs? Check out [morrislaptop/popo-factory](https://github.com/morrislaptop/popo-factory)

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Craig Morris](https://github.com/morrislaptop)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

54

—

FairBetter than 96% of packages

Maintenance80

Actively maintained with recent releases

Popularity46

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 87% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~104 days

Recently: every ~279 days

Total

19

Last Release

104d ago

PHP version history (3 changes)v0.1.0PHP ^8.0

v0.3.1PHP ^7.4|^8.0

v0.14.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/ecf933ed5c8b3e08c1880734166cb32d9e66eb50b01bb91ff74018c3e4b61058?d=identicon)[morrislaptop](/maintainers/morrislaptop)

---

Top Contributors

[![morrislaptop](https://avatars.githubusercontent.com/u/67807?v=4)](https://github.com/morrislaptop "morrislaptop (47 commits)")[![eithed](https://avatars.githubusercontent.com/u/795678?v=4)](https://github.com/eithed "eithed (3 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![chojnicki](https://avatars.githubusercontent.com/u/34774522?v=4)](https://github.com/chojnicki "chojnicki (1 commits)")[![joelbutcher](https://avatars.githubusercontent.com/u/7163152?v=4)](https://github.com/joelbutcher "joelbutcher (1 commits)")

---

Tags

morrislaptoplaravel-popo-caster

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/morrislaptop-laravel-popo-caster/health.svg)

```
[![Health](https://phpackages.com/badges/morrislaptop-laravel-popo-caster/health.svg)](https://phpackages.com/packages/morrislaptop-laravel-popo-caster)
```

###  Alternatives

[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M339](/packages/api-platform-core)[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[api-platform/symfony

Symfony API Platform integration

384.5M129](/packages/api-platform-symfony)[api-platform/serializer

API Platform core Serializer

274.8M87](/packages/api-platform-serializer)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1616.4k13](/packages/2lenet-crudit-bundle)[web-auth/webauthn-lib

FIDO2/Webauthn Support For PHP

12310.5M135](/packages/web-auth-webauthn-lib)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
