PHPackages                             think.studio/laravel-json-field-cast - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. think.studio/laravel-json-field-cast

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

think.studio/laravel-json-field-cast
====================================

Cast json field to custom object.

2.3.0(2y ago)116814MITPHPPHP ^8.1

Since Feb 22Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/dev-think-one/laravel-json-field-cast)[ Packagist](https://packagist.org/packages/think.studio/laravel-json-field-cast)[ Docs](https://github.com/dev-think-one/laravel-json-field-cast)[ RSS](/packages/thinkstudio-laravel-json-field-cast/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (6)Versions (14)Used By (14)

Laravel json field cast
=======================

[](#laravel-json-field-cast)

[![Packagist License](https://camo.githubusercontent.com/aed9e6a62bdfc8e72bfadcf3e317b78f7b2c11a1a95a7e0c2fd8563f0e4c7965/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7468696e6b2e73747564696f2f6c61726176656c2d6a736f6e2d6669656c642d636173743f636f6c6f723d253233346463373166)](https://camo.githubusercontent.com/aed9e6a62bdfc8e72bfadcf3e317b78f7b2c11a1a95a7e0c2fd8563f0e4c7965/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7468696e6b2e73747564696f2f6c61726176656c2d6a736f6e2d6669656c642d636173743f636f6c6f723d253233346463373166)[![Packagist Version](https://camo.githubusercontent.com/5a209e52278918940a63a8df828579f09dab7f8054fb4bdbcb806381b9ddd6ba/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7468696e6b2e73747564696f2f6c61726176656c2d6a736f6e2d6669656c642d63617374)](https://packagist.org/packages/think.studio/laravel-json-field-cast)[![Total Downloads](https://camo.githubusercontent.com/4c91217c1dd35337fa87ed28f0899eb701ba0f4b1692bb286eceeae422c79144/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7468696e6b2e73747564696f2f6c61726176656c2d6a736f6e2d6669656c642d63617374)](https://packagist.org/packages/think.studio/laravel-json-field-cast)[![Build Status](https://camo.githubusercontent.com/e46404155899b1e3b7078f0cb394f37e70244880816190d44a7f58d3f2c647e1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6465762d7468696e6b2d6f6e652f6c61726176656c2d6a736f6e2d6669656c642d636173742f6261646765732f6275696c642e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/dev-think-one/laravel-json-field-cast/build-status/main)[![Code Coverage](https://camo.githubusercontent.com/a30cf3eb60eb82fb1947ce966f94cb414e258bcdcc9627a24782c0857110d4cc/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6465762d7468696e6b2d6f6e652f6c61726176656c2d6a736f6e2d6669656c642d636173742f6261646765732f636f7665726167652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/dev-think-one/laravel-json-field-cast/?branch=main)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d7ae18b7d061ba6fd05ad48b22027ab4b11fc7d17dae4b3e5884a878ee3d7297/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6465762d7468696e6b2d6f6e652f6c61726176656c2d6a736f6e2d6669656c642d636173742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/dev-think-one/laravel-json-field-cast/?branch=main)

Cast json field to custom object. Compatible with Laravel 9, 10, and 11.

Requirements
------------

[](#requirements)

- PHP: ^8.1|^8.2
- Laravel: ^9.0|^10.0|^11.0

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

[](#installation)

Install the package via composer:

```
composer require think.studio/laravel-json-field-cast
```

Usage
-----

[](#usage)

### Out of the box

[](#out-of-the-box)

Cast json column to single object

```
/**
 *  @property \JsonFieldCast\Json\SimpleJsonField $json_meta
 *  @property \JsonFieldCast\Json\SimpleJsonField $text_meta
 */
class MyModel extends Model
{
    // Laravel 9/10/11 approach with $casts property
    protected $casts = [
        //...
        'json_meta'              => \JsonFieldCast\Casts\SimpleJsonField::class,
        'text_meta'              => \JsonFieldCast\Casts\SimpleJsonField::class,
    ];

    // Alternative Laravel 11+ approach with casts() method
    protected function casts(): array
    {
        return [
            //...
            'json_meta'              => \JsonFieldCast\Casts\SimpleJsonField::class,
            'text_meta'              => \JsonFieldCast\Casts\SimpleJsonField::class,
        ];
    }
}

$myModel = MyModel::find(123);
$myModel->json_meta->getAttribute('example', 'default');
$myModel->json_meta->getAttribute('my_array.3',);
$myModel->json_meta->getAttribute('my_array.test');
$myModel->json_meta->setAttribute('position', 'developer');
$myModel->json_meta->removeAttribute('position');
$myModel->json_meta->hasAttribute('position');
$myModel->json_meta->getRawData(['position', 'my_array']);

$myModel->json_meta->setDate('my-date', \Carbon\Carbon::now());
$myModel->json_meta->setDate('my-date', \Carbon\Carbon::now(), 'd/m/y');
$myModel->json_meta->setNow('my-date', 'd/m/y');
$myModel->json_meta->getDateAttribute('my-date'); // Carbon::parse()
$myModel->json_meta->getDateTimeFromFormat('my-date', 'd/m/y');
$myModel->json_meta->getDateTimeFromFormats('my-date', ['d/m/y', 'd/m/Y', 'Y-m-d']);

$myModel->json_meta->inscremt('login_count');
$myModel->json_meta->decrement('allowed_attempts');

$myModel->json_meta->toMorph('user', $user);
$user = $myModel->json_meta->fromMorph('user');
```

Cast json column to array of objects

```
/**
 *  @property \JsonFieldCast\Json\ArrayOfJsonObjectsField $array_json_meta
 *  @property \JsonFieldCast\Json\ArrayOfJsonObjectsField $array_text_meta
 */
class MyModel extends Model
{
    // Laravel 9/10/11 approach with $casts property
    protected $casts = [
        //...
        'array_json_meta'        => \JsonFieldCast\Casts\ArrayOfJsonObjectsField::class,
        'array_text_meta'        => \JsonFieldCast\Casts\ArrayOfJsonObjectsField::class,
    ];

    // Alternative Laravel 11+ approach with casts() method
    protected function casts(): array
    {
        return [
            //...
            'array_json_meta'        => \JsonFieldCast\Casts\ArrayOfJsonObjectsField::class,
            'array_text_meta'        => \JsonFieldCast\Casts\ArrayOfJsonObjectsField::class,
        ];
    }
}

$myModel = MyModel::find(123);

isset($myModel->array_json_meta[2]);

/** @var \JsonFieldCast\Json\JsonObject $item */
foreach ($myModel->array_json_meta as $item) {
    $name = $item->getAttribute('name');
    $date = $item->getDateTimeFromFormat('my-date', 'd/m/y');
}
```

### Custom castable objects

[](#custom-castable-objects)

For your custom purposes you can use your own custom castable objects

```
namespace App\Casts;

use JsonFieldCast\Casts\AbstractMeta;

class FormMeta extends AbstractMeta
{
    protected function metaClass(): string
    {
        return \App\Casts\Json\FormMeta::class;
    }
}
```

```
namespace App\Casts\Json;

use JsonFieldCast\Json\AbstractMeta;

class FormMeta extends AbstractMeta
{
    public function myCustomMethod(): int {
        return ((int) $this->getAttribute('foo.bar', 0)) + 25;
    }
}
```

```
/**
 *  @property \App\Casts\Json\FormMeta $meta
 */
class Form extends Model
{
    // Laravel 9/10/11 approach with $casts property
    protected $casts = [
        'meta' => \App\Casts\FormMeta::class,
    ];

    // Alternative Laravel 11+ approach with casts() method
    protected function casts(): array
    {
        return [
            'meta' => \App\Casts\FormMeta::class,
        ];
    }
}

$form = Form::find(123);
$form->meta->getAttribute('foo.bar', 0);
$form->meta->myCustomMethod();
```

### Dynamic castable objects

[](#dynamic-castable-objects)

```
namespace App\Casts;

use JsonFieldCast\Casts\AbstractMeta;

class FormMeta extends AbstractMeta
{
    protected function metaClass(): string
    {
        return \App\Casts\Json\AbstractFormMeta::class;
    }
}
```

```
namespace App\Casts\Json;

use JsonFieldCast\Json\AbstractMeta;

abstract class AbstractFormMeta extends AbstractMeta
{
   public static function getCastableClassByModel(Model $model, array $data = []): ?string
    {
        return ($model->meta_type && class_exists($model->meta_type))
            ? $model->meta_type
            : null;;
    }
}
```

```
/**
 *  @property \App\Casts\Json\AbstractFormMeta $meta
 */
class Form extends Model
{
    // Laravel 9/10/11 approach with $casts property
    protected $casts = [
        'meta' => \App\Casts\FormMeta::class,
    ];

    // Alternative Laravel 11+ approach with casts() method
    protected function casts(): array
    {
        return [
            'meta' => \App\Casts\FormMeta::class,
        ];
    }
}

$formContactUs = Form::create([
    //...
    'meta_type' => ContactUsMeta::class
]);
$formJobRequest = Form::create([
    //...
    'meta_type' => JobRequestMeta::class
]);

$formContactUs->meta instanceof ContactUsMeta::class // true
$formJobRequest->meta instanceof JobRequestMeta::class // true
```

Credits
-------

[](#credits)

- [![Think Studio](https://camo.githubusercontent.com/8e541bece07d503c85a126b5294865faa00e27371048772f566a0cce8c01fd3a/68747470733a2f2f7961726f736c617777772e6769746875622e696f2f696d616765732f73706f6e736f72732f7061636b616765732f6c6f676f2d7468696e6b2d73747564696f2e706e67)](https://think.studio/)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 93.5% 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 ~42 days

Recently: every ~31 days

Total

13

Last Release

1037d ago

Major Versions

1.2.0 → 2.0.02022-10-05

PHP version history (2 changes)1.1.3PHP ^8.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/53f93fa87d58f33d106de6bd5e2946f8a345ebfaee146360746056cb134a15a0?d=identicon)[think.studio](/maintainers/think.studio)

---

Top Contributors

[![yaroslawww](https://avatars.githubusercontent.com/u/23663794?v=4)](https://github.com/yaroslawww "yaroslawww (29 commits)")[![annromantsova](https://avatars.githubusercontent.com/u/16173671?v=4)](https://github.com/annromantsova "annromantsova (2 commits)")

---

Tags

jsonlaravelfieldcast

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/thinkstudio-laravel-json-field-cast/health.svg)

```
[![Health](https://phpackages.com/badges/thinkstudio-laravel-json-field-cast/health.svg)](https://phpackages.com/packages/thinkstudio-laravel-json-field-cast)
```

###  Alternatives

[novadaemon/filament-pretty-json

Read-only field to show pretty json in your filamentphp forms

44359.2k2](/packages/novadaemon-filament-pretty-json)[stepanenko3/nova-json

Nova json field to spread a json column throughout multiple fields.

42249.7k](/packages/stepanenko3-nova-json)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[json-mapper/laravel-package

The JsonMapper package for Laravel

25170.4k3](/packages/json-mapper-laravel-package)[garf/laravel-conf

Store additional configs in JSON or Database (write, read)

246.4k](/packages/garf-laravel-conf)

PHPackages © 2026

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