PHPackages                             dimalebid/laravel-json-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. dimalebid/laravel-json-cast

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

dimalebid/laravel-json-cast
===========================

description

1.0.2(4y ago)021MITPHPPHP ^7.4|^8.0

Since Apr 28Pushed 4y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (5)Used By (0)

Laravel Json Cast
=================

[](#laravel-json-cast)

[![Latest Version on Packagist](https://camo.githubusercontent.com/537afee63edd14833b45b764878da27745e8499ff4df956060ee7cf9158feb5c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64696d616c656269642f6c61726176656c2d6a736f6e2d636173742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dimalebid/laravel-json-cast)[![Total Downloads](https://camo.githubusercontent.com/faf1377840a2e776c05b6f9f5bae8cc055701c2ba4145c8d9d0029f838d42f53/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64696d616c656269642f6c61726176656c2d6a736f6e2d636173742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dimalebid/laravel-json-cast)[![Packagist License](https://camo.githubusercontent.com/64050fe16603acb32791085054ac44a6d674a766c8b5410b95adaae6426ba744/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64696d616c656269642f6c61726176656c2d6a736f6e2d63617374)](https://camo.githubusercontent.com/64050fe16603acb32791085054ac44a6d674a766c8b5410b95adaae6426ba744/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64696d616c656269642f6c61726176656c2d6a736f6e2d63617374)

Вы когда-нибудь хотели привести свои столбцы JSON к объекту-значению?

Под капотом он реализует интерфейс Laravel Arrayable, которое обрабатывает сериализацию между JsonCastable и столбцом базы данных JSON.

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

[](#installation)

You can install the package via composer:

```
$ composer require dimalebid/laravel-json-cast
```

Usage
-----

[](#usage)

### 1. Создаете `UserSettings`

[](#1-создаете-usersettings)

```
namespace App\Models\User;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use App\Models\User\Settings\Settings;

class UserSettings implements CastsAttributes
{

    public function get($model, string $key, $value, array $attributes)
    {
        return new Settings((array)json_decode($value, true));
    }

    public function set($model, string $key, $value, array $attributes)
    {
        return json_encode(get_object_vars($value));
    }
}
```

### 2. Создаете `Settings`

[](#2-создаете-settings)

```
namespace App\Models\User\Settings;

use DimaLebid\LaravelJsonCast\JsonCastable;
use App\Models\User\Settings\Auth;

class Settings extends JsonCastable
{
    public Auth $auth;
    public string $country = 'Ukraine';
    public ?string $phone;

    public function __construct(array $attributes)
    {
        $this->auth = new Auth($attributes);
        parent::__construct($attributes);
    }
}
```

Обьект `Auth` похож на `Settings` с одним отличием что в нем нет конструктора.

### 3. Настройте свой атрибут Eloquent для приведения к нему:

[](#3-настройте-свой-атрибут-eloquent-для-приведения-к-нему)

```
namespace App\Models;

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

class User extends Model
{
    protected $casts = [
        'settings' => UserSettings::class,
    ];
}
```

Получить значение из поля JSON
------------------------------

[](#получить-значение-из-поля-json)

```
$userId   = 1;
$user     = User::find($userId);
$settings = $user->settings;

dd($settings->country);
//or
dd($settings->auth->facebook);
```

Сохранить или обновить значение в поле JSON
-------------------------------------------

[](#сохранить-или-обновить-значение-в-поле-json)

```
$userId = 1;
$user   = User::find($userId);

$user->settings->country = 'USA';
$user->save();
```

Вот так легко и просто можно работать с полем JSON.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

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

Total

3

Last Release

1474d ago

PHP version history (2 changes)1.0.0PHP ^7.2|^8.0

1.0.2PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f468b9d934c6f4870db4089ff4c1daa7186309648f98f87822ffb2b30b5fff7?d=identicon)[dimalebid](/maintainers/dimalebid)

---

Top Contributors

[![dimalebid](https://avatars.githubusercontent.com/u/39432587?v=4)](https://github.com/dimalebid "dimalebid (10 commits)")

---

Tags

laravelLaravel CastLaravel Cast JsonJson Castable

### Embed Badge

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

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

###  Alternatives

[highideas/laravel-users-online

This package will provide an online users management.

203113.2k1](/packages/highideas-laravel-users-online)[stephenjude/filament-blog

Filament Blog Builder

20317.8k](/packages/stephenjude-filament-blog)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[datomatic/nova-detached-actions

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

11229.2k](/packages/datomatic-nova-detached-actions)

PHPackages © 2026

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