PHPackages                             tailflow/castable-dto - 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. tailflow/castable-dto

Abandoned → [tailflow/dto](/?search=tailflow%2Fdto)Library[Utility &amp; Helpers](/categories/utility)

tailflow/castable-dto
=====================

Automatically cast JSON columns to PHP objects in Laravel using Spatie's data-transfer-object class

2.1.0(4y ago)829.5k2MITPHPPHP &gt;=8.0

Since Nov 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/tailflow/castable-dto)[ Packagist](https://packagist.org/packages/tailflow/castable-dto)[ Docs](https://github.com/tailflow/castable-dto)[ RSS](/packages/tailflow-castable-dto/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (4)Dependencies (4)Versions (5)Used By (0)

Laravel Castable Data Transfer Object
=====================================

[](#laravel-castable-data-transfer-object)

### Deprecated

[](#deprecated)

Please consider using [tailflow/dto](https://github.com/tailflow/dto) instead.

[![Latest Version on Packagist](https://camo.githubusercontent.com/bf4d9b2d5900423f8e54deb797fec65e3ddcc47269d56bf8dcf98506e2678ade/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7461696c666c6f772f6361737461626c652d64746f2e737667)](https://packagist.org/packages/tailflow/castable-dto)[![Build Status on GitHub Actions](https://camo.githubusercontent.com/639f99dfc8cb834dbcf3f9c7a4ad5de0ee909e4e8f1ab061e006cc1def84a715/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7461696c666c6f772f6361737461626c652d64746f2f63692e796d6c3f6272616e63683d6d61696e)](https://github.com/tailflow/castable-dto/actions)

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

This package gives you an extended version of Spatie's `DataTransferObject` class, called `CastableDataTransferObject`.

Under the hood it implements Laravel's [`Castable`](https://laravel.com/docs/8.x/eloquent-mutators#castables) interface with a Laravel [custom cast](https://laravel.com/docs/7.x/eloquent-mutators#custom-casts) that handles serializing between the `DataTransferObject` (or a compatible array) and your JSON database column.

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

[](#installation)

You can install the package via composer:

```
composer require tailflow/castable-dto
```

Usage
-----

[](#usage)

### 1. Create your `CastableDataTransferObject`

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

Check out the readme for Spatie's [data transfer object](https://github.com/spatie/data-transfer-object) package to find out more about what their `DataTransferObject` class can do.

```
namespace App\DataTansferObjects;

use Tailflow\DataTransferObjects\CastableDataTransferObject;

class Address extends CastableDataTransferObject
{
    public string $country;
    public string $city;
    public string $street;
}
```

### 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.

```
namespace App\Models;

use App\DataTansferObjects\Address;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'address' => 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' => [
        'country' => 'Japan',
        'city' => 'Tokyo',
        'street' => '4-2-8 Shiba-koen',
    ],
]);

$residents = User::where('address->city', 'Tokyo')->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);

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

Credits
-------

[](#credits)

- Original [package](https://github.com/jessarcher/laravel-castable-data-transfer-object) by [Jess Archer](https://github.com/jessarcher)

License
-------

[](#license)

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

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~158 days

Total

4

Last Release

1573d ago

Major Versions

v1.0.1 → 2.0.02021-06-14

PHP version history (2 changes)1.0PHP &gt;=7.4

2.0.0PHP &gt;=8.0

### Community

Maintainers

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

---

Top Contributors

[![alexzarbn](https://avatars.githubusercontent.com/u/12220926?v=4)](https://github.com/alexzarbn "alexzarbn (18 commits)")

---

Tags

laravel-castable-data-transfer-objectlaravel-castable-value-objectlaravel-data-transfer-objecttailflowlaravel castable dtolaravel value object

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tailflow-castable-dto/health.svg)

```
[![Health](https://phpackages.com/badges/tailflow-castable-dto/health.svg)](https://phpackages.com/packages/tailflow-castable-dto)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[illuminate/collections

The Illuminate Collections package.

27075.6M1.0k](/packages/illuminate-collections)[illuminate/config

The Illuminate Config package.

10943.9M2.4k](/packages/illuminate-config)[illuminate/pipeline

The Illuminate Pipeline package.

9348.3M267](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10533.5M989](/packages/illuminate-pagination)[illuminate/redis

The Illuminate Redis package.

8314.4M362](/packages/illuminate-redis)

PHPackages © 2026

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