2017 © Pedro Peláez
 

library laravel-model-json

Provides JSON column support for for Laravel's Eloquent Model.

image

hnhdigital-os/laravel-model-json

Provides JSON column support for for Laravel's Eloquent Model.

  • Tuesday, May 15, 2018
  • by bluora
  • Repository
  • 1 Watchers
  • 22 Stars
  • 721 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 22 Versions
  • 18 % Grown

The README.md

    __                                __    _______ ____  _   __
   / /   ____ __________ __   _____  / /   / / ___// __ \/ | / /
  / /   / __ `/ ___/ __ `/ | / / _ \/ /_  / /\__ \/ / / /  |/ / 
 / /___/ /_/ / /  / /_/ /| |/ /  __/ / /_/ /___/ / /_/ / /|  /  
/_____/\__,_/_/   \__,_/ |___/\___/_/\____//____/\____/_/ |_/   

Adds support for the JSON datatype column for models via an object based interface., (*1)

Latest Stable Version Total Downloads Latest Unstable Version Built for Laravel License, (*2)

Build Status StyleCI Test Coverage Issue Count Code Climate, (*3)

This package has been developed by H&H|Digital, an Australian botique developer. Visit us at hnh.digital., (*4)

Install

$ composer require hnhdigital-os/laravel-model-json ~1.0, (*5)

Usage

Basic

The feature is exposed through a trait that allows you to define columns that contain JSON data. When the model is created, it generates methods using the specified column names. You can then get and set the attributes directly., (*6)

use Bluora\LaravelModelJson\JsonColumnTrait;

class User extends Model
{
    use JsonColumnTrait;

    protected $json_columns = [
        'settings'
    ];
}

The JSON column values can then be retrieved or set via an object property., (*7)

Let's say we have an array of data stored in the settings JSON column:, (*8)

['showProfilePicture' => true, 'options' => ['option1' => 'red']];

Getting these values is as simple as:, (*9)

echo $user->settings()->showProfilePicture."\n";
echo $user->settings()->options['option1'];

Would output:, (*10)

1
red

You can update any variable or add a new one:, (*11)

$user->settings()->options['option2'] = 1;
$user->save();

And would update the JSON object with the following array:, (*12)

['showProfilePicture' => true, 'options' => ['option1' => 'red', 'option2' => 1]];

Calling getDirty with true will provide changes using dot notation., (*13)

print_r($user->getDirty(true));

Would output:, (*14)

array(
    'settings' => "{"showProfilePicture":true,"options":{"option1":"red","option2":1}}",
    'settings.options' => array('option2' => 1)
)

NOTE

If you use findOrNew, firstOrNew, firstOrCreate, or the updateOrCreate method, you should run the inspectJson method before using any JSON columns as the newFromBuilder method (which we override) is not called on new model objects., (*15)

$model = Model::firstOrNew(['name' => 'example']);
$model->inspectJson();

Defaults

You can define default values for a json attribute by using the $json_defaults property on the model., (*16)

You specify the attribute name and default value, if the name does not exist, it will be added at the creation of the object., (*17)

protected $json_defaults = [
    'settings' => ['showProfilePicture' => 0]
];

Saving changes

When a save event has been called, the trait sets the original attribute value with the latest JSON encoded value., (*18)

If you have used defaults, you can stop these from being saved to the database by setting the option no_saving_default_values to true for the specific json column, (*19)

protected $json_options = [
    'settings' => ['no_saving_default_values' => true]
];

Contributing

Please see CONTRIBUTING for details., (*20)

Credits

License

The MIT License (MIT). Please see License File for more information., (*21)

The Versions

15/05 2018

dev-master

9999999-dev

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

15/05 2018

v1.4.13

1.4.13.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

15/05 2018

dev-analysis-q1m0a1

dev-analysis-q1m0a1

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

14/05 2018

dev-develop

dev-develop

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

14/05 2018

v1.4.12

1.4.12.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

14/05 2018

v1.4.11

1.4.11.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

12/09 2017

dev-analysis-qMxw3l

dev-analysis-qMxw3l

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

12/09 2017

v1.4.10

1.4.10.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

05/04 2017

v1.4.9

1.4.9.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

26/10 2016

v1.4.8

1.4.8.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

14/09 2016

v1.4.7

1.4.7.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

25/08 2016

v1.4.6

1.4.6.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

21/06 2016

v1.4.5

1.4.5.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

20/06 2016

v1.4.3

1.4.3.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

20/06 2016

v1.4.4

1.4.4.0

Provides JSON column support for for Laravel's Eloquent Model.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

24/05 2016

v1.4.2

1.4.2.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

24/05 2016

v1.4.1

1.4.1.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

25/03 2016

v1.4

1.4.0.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

18/03 2016

v1.3

1.3.0.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

18/03 2016

v1.2

1.2.0.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

18/03 2016

v1.1

1.1.0.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free

18/03 2016

v1.0

1.0.0.0

Provides JSON datatype support for the Eloquent ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel json eloquent illuminate schemaless schema-free