PHPackages                             denismitr/laravel-json-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. [Database &amp; ORM](/categories/database)
4. /
5. denismitr/laravel-json-attributes

ActiveLibrary[Database &amp; ORM](/categories/database)

denismitr/laravel-json-attributes
=================================

Laravel Json Attributes for Eloquent Models

v2.0(6y ago)72661MITPHPPHP &gt;=7.2

Since May 25Pushed 6y ago1 watchersCompare

[ Source](https://github.com/denismitr/laravel-json-attributes)[ Packagist](https://packagist.org/packages/denismitr/laravel-json-attributes)[ RSS](/packages/denismitr-laravel-json-attributes/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (6)Dependencies (5)Versions (9)Used By (1)

Laravel Json Attributes for Eloquent Models
===========================================

[](#laravel-json-attributes-for-eloquent-models)

[![Build Status](https://camo.githubusercontent.com/9c75df30880493ef8683fddd88e5999055d0ee5acd605036d81e0252730c1cca/68747470733a2f2f7472617669732d63692e6f72672f64656e69736d6974722f6c61726176656c2d6a736f6e2d617474726962757465732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/denismitr/laravel-json-attributes)

Author
------

[](#author)

Denis Mitrofanov
[thecollection.ru](https://thecollection.ru)

### Requirements

[](#requirements)

PHP 7.2 or higher MYSQL 5.7 or higher or POSTGRES (the one I tested it on is 9.6) probably any 9.\* version or higher will do

### Installation

[](#installation)

`composer require denismitr/laravel-json-attributes`

In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in `config/app.php` file:

```
'providers' => [
    // ...
    Denismitr\JsonAttributes\JsonAttributesServiceProvider::class,
];

```

### Usage

[](#usage)

1. First add a `jsonData` column to your table

```
Schema::create('orders', function (Blueprint $table) {
    $table->increments('id');
    $table->string('description');
    $table->jsonData('json_data');
});
```

Underneath `$table->jsonData('json_data');` is just `$table->json('json_data')->nullable()`.

Examples of usage:

```
$array = [
    'supplier' => 'Boeing',
    'total_cost' => 245.99,
];

$record = Record::create(['json_data' => $array]);

$this->assertEquals($array, $record->json_data->all());
$this->assertEquals('Boeing', $record->json_data->supplier);
$this->assertEquals('Boeing', $record->json_data['supplier']);
```

Another examples with Laravel dot notation

```
$this->record->json_data->member = ['name' => 'John', 'age' => 30];
$this->record->json_data->forget('member.age');
$this->assertEquals($this->record->json_data->member, ['name' => 'John']);
```

```
$this->record->json_data->set('settings.connection', 'mysql');
$this->record->json_data->set('colors.navbar', 'dark');

$this->assertEquals('mysql', $this->record->json_data->get('settings.connection'));
$this->assertEquals('dark', $this->record->json_data->get('colors.navbar'));
```

You can assign a whole array

```
$array = [
    'one' => 'value',
    'two' => 'another value',
];

$this->record->json_data->array = $array;

$this->assertEquals($array, $this->record->json_data->array);
```

### Searching

[](#searching)

Since version `1.0` nested search is available

```
$recordA = Record::create(['json_data' => [
    'company' => 'Ecommelite',
    'user' => [
        'name' => 'Denis',
        'job_title' => 'developer'
    ]
]]);

$recordB = Record::create(['json_data' => [
    'company' => 'Ecommelite',
    'user' => [
        'name' => 'Tom',
        'job_title' => 'developer'
    ]
]]);

$recordC = Record::create(['json_data' => [
    'company' => 'Ecommelite',
    'address' => [
        'street' => '1st Street',
        'phone' => 1234556
    ]
]]);

$this->assertContainsModels(
    [$recordA],
    Record::withJsonData(['user.name' => 'Denis'])->get()
);

$this->assertContainsModels(
    [$recordA, $recordB],
    Record::withJsonData(['user.job_title' => 'developer'])->get()
);

$this->assertContainsModels(
    [$recordA, $recordB],
    Record::withJsonData(['user.job_title' => 'developer', 'company' => 'Ecommelite'])->get()
);

$this->assertContainsModels(
    [$recordA],
    Record::withJsonData([
        'user.job_title' => 'developer',
        'company' => 'Ecommelite',
        'user.name' => 'Denis'
    ])->get()
);

$this->assertContainsModels(
    [$recordA, $recordB],
    Record::withJsonData('user.job_title', 'developer')->get()
);

$this->assertContainsModels(
    [], Record::withJsonData(['non.existent' => 'record'])->get()
);
```

#### To see more look at the tests.

[](#to-see-more-look-at-the-tests)

### Eloquent Model

[](#eloquent-model)

First of all you need to cast the *json attributes* to array Here is how the `Record` model from test suite looks like:

```
use Denismitr\JsonAttributes\JsonAttributes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Record extends Model
{
    protected $casts = ['json_data' => 'array'];

    /**
     * @return JsonAttributes
     */
    public function getJsonDataAttribute(): JsonAttributes
    {
        return JsonAttributes::create($this, 'json_data');
    }

    /**
     * @return Builder
     */
    public function scopeWithJsonData(): Builder
    {
        return JsonAttributes::scopeWithJsonAttributes('json_data');
    }
}
```

Persistence works regularly just like with any other Eloquent model

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

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

Recently: every ~86 days

Total

8

Last Release

2490d ago

Major Versions

v0.4 → v1.0.x-dev2018-08-15

v1.0 → v2.0.x-dev2019-07-20

PHP version history (4 changes)0.1PHP ^7.1

v0.3PHP &gt;=7.1

v0.4PHP &gt;=7.0

v2.0.x-devPHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d68ae5d5ca94f49a69f961a7825865d92247b09e276a25fcc6ad485d4c8c964?d=identicon)[denismitr](/maintainers/denismitr)

---

Top Contributors

[![denismitr](https://avatars.githubusercontent.com/u/16356446?v=4)](https://github.com/denismitr "denismitr (28 commits)")

---

Tags

eloquentjsonlaraveljsonlaraveleloquentdenismitrjson-attributes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/denismitr-laravel-json-attributes/health.svg)

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

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[io238/laravel-iso-countries

Ready-to-use Laravel models and relations for country (ISO 3166), language (ISO 639-1), and currency (ISO 4217) information with multi-language support.

5462.3k](/packages/io238-laravel-iso-countries)

PHPackages © 2026

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