Download the PHP package bluora/laravel-model-json without Composer
On this page you can find all versions of the php package bluora/laravel-model-json. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bluora/laravel-model-json
More information about bluora/laravel-model-json
Files in bluora/laravel-model-json
Package laravel-model-json
Short Description Provides JSON column support for for Laravel's Eloquent Model.
License MIT
Informations about the package laravel-model-json
Adds support for the JSON datatype column for models via an object based interface.
This package has been developed by H&H|Digital, an Australian botique developer. Visit us at hnh.digital.
Install
$ composer require hnhdigital-os/laravel-model-json ~1.0
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.
The JSON column values can then be retrieved or set via an object property.
Let's say we have an array of data stored in the settings
JSON column:
Getting these values is as simple as:
Would output:
You can update any variable or add a new one:
And would update the JSON object with the following array:
Calling getDirty
with true
will provide changes using dot notation.
Would output:
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.
Defaults
You can define default values for a json attribute by using the $json_defaults
property on the model.
You specify the attribute name and default value, if the name does not exist, it will be added at the creation of the object.
Saving changes
When a save event has been called, the trait sets the original attribute value with the latest JSON encoded value.
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
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.