PHPackages                             yonchando/laravel-cast-attributes - 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. yonchando/laravel-cast-attributes

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

yonchando/laravel-cast-attributes
=================================

Laravel model cast json columns to class object

v1.0.1(1y ago)0139MITPHPPHP ^8.0

Since Jun 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/yonchando/laravel-cast-attributes)[ Packagist](https://packagist.org/packages/yonchando/laravel-cast-attributes)[ Docs](https://github.com/yonchando/laravel-cast-attributes)[ RSS](/packages/yonchando-laravel-cast-attributes/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (8)Versions (3)Used By (0)

Laravel Cast Attribute
======================

[](#laravel-cast-attribute)

[![Latest Version on Packagist](https://camo.githubusercontent.com/651f32b745c93e9cc7d305338450f8cf78f6e190630303eb12bc30979b0a5409/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796f6e6368616e646f2f6c61726176656c2d636173742d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yonchando/laravel-cast-attributes)[![Total Downloads](https://camo.githubusercontent.com/59ddb8e7e275b2592991662be97304e71a89d8bf76e703cb97ec2d683253819f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796f6e6368616e646f2f6c61726176656c2d636173742d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yonchando/laravel-cast-attributes)

Laravel cast attribute is model custom cast json column to Class entity

> ***IMPORTANT*** property is not primitive (User-Define Type), The User-Define type need to have default value to property.

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

[](#installation)

Via Composer

```
composer require yonchando/laravel-cast-attributes
```

Usage
-----

[](#usage)

**Model**

```
class User extends \Illuminate\Database\Eloquent\Model
{
    protected $fillable = ['json'];

    protected $casts = [
        'json' => UserJson::class
    ];
}
```

**UserJson**

> **private property must have setter with camelCase start with `set`**

```
class UserJson extends \Yonchando\CastAttributes\CastAttributes
{
    private string $firstName;

    protected string $lastName;

    public string $gender;

    public Image $image;

    public  function getFirstName(): string
    {
        return $this->firstName;
    }

    public  function setFirstName(string $firstName):void
    {
        $this->firstName = $firstName;
    }

    public function getLastName(){
        return $this->lastName;
    }
}
```

**Image**

> For sub property class use trait `CastProperty`

```
class Image
{
    use \Yonchando\CastAttributes\Traits\CastProperty;

    public string $filename;
    public int $size;
    public string $path;

    public function url()
    {
        return Storage::url($this->path);
    }
}
```

**Controller**

```
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function show($id)
    {
        $user = User::find($id);

        $user->json->getFirstName();
        $user->json->getLastName();
        $user->json->gender;

        // access function in image class
        $user->json->image->url();

        // to array
        $user->json->toArray();
        $user->json->image->toArray();

    }

    public function store(Request $request)
    {
        // use class
        $user = new User([
            'json' => UserJson::create($request->all()),
        ]);

        // use an array
        $user = User::create([
            'json' => [
                'first_name' => $request->get('first_name'),
                'last_name' => $request->get('last_name'),
                'gender' => $request->get('gender'),
                'image' => $request->get('image'),
            ]
        ])
    }
}
```

Methods available for CastProperty trait
----------------------------------------

[](#methods-available-for-castproperty-trait)

NamemodifierparametersDescriptiontoArraypublicclass property to arraytoJsonpublicint $optionsclass property to json\_encode stringcreatepublic staticarray|object $datainitialize classChange log
----------

[](#change-log)

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

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

[](#contributing)

Please see [contributing.md](contributing.md) for details and a todolist.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Author Name](https://github.com/yonchando)
- [All Contributors](../../contributors)

License
-------

[](#license)

MIT. Please see the [license file](license.md) for more information.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance42

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 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 ~287 days

Total

2

Last Release

460d ago

### Community

Maintainers

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

---

Top Contributors

[![yonchando](https://avatars.githubusercontent.com/u/26330343?v=4)](https://github.com/yonchando "yonchando (6 commits)")

---

Tags

laravellaravel-castinglaravel cast attributecast objectcast jsonlaravel json column

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/yonchando-laravel-cast-attributes/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

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

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.6k](/packages/larastan-larastan)[laravel/ai

The official AI SDK for Laravel.

9782.1M162](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

59156.3k11](/packages/api-platform-laravel)[illuminate/queue

The Illuminate Queue package.

20432.2M1.5k](/packages/illuminate-queue)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45344.0k1](/packages/pressbooks-pressbooks)

PHPackages © 2026

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