Skip to content
/ dialect Public
forked from darrylkuhn/dialect

Provides JSON datatype support for the Eloquent ORM

License

Notifications You must be signed in to change notification settings

roquie/dialect

 
 

Repository files navigation

Dialect

Build Status Code Climate Test Coverage

Dialect provides JSON datatype support for the Eloquent ORM. At this point this implementation is pretty bare bones and has been demonstrated to work with PostgreSQL and MySQL. There are lots of opportunities to enhance and improve. If you're interested in contributing please submit merge/pull requests.

Installation

Require this package in your composer.json file:

"darrylkuhn/dialect": "dev-master"

...then run composer update to download the package to your vendor directory.

Usage

The feature is exposed through a trait called which allows you to define attributes on the model which are of the json datatype. When the model is read in it will parse the JSON document and set up getters and setters for each top level attribute making it easy to interact with the various attributes within the document. For example we could create a Photos model like this:

class Photo extends Eloquent
{
    use Eloquent\Dialect\Json;
    protected $jsonColumns = ['json_data'];
}

And then this:

$attr = json_decode($photo->json_data);
$attr->key = $value;
$photo->json_data = json_encode($attr);

becomes this:

$photo->key = value;

Also when calling the toArray() method the attributes are moved to the top level and the 'json_attributes' column is hidden. This essentially hides away the fact that you're using the json datatype and makes it look like we're working with attributes directly.

You can also establish relationships on a model like this (only supported in PostgreSQL):

public function user()
{
    return $this->hasOne( 'User', 'id', "json_data->>'user_id'" );
}

The one caveat is when you're setting an attribute not previously set already in the JSON document then no setter is created. For example if you defined a json column called "additional_details" which had a value of "{'foo':'bar'}" and wanted to set 'fizz' so you would need to call:

$referral->setJsonAttribute( 'additional_details', 'fizz', 'buzz' );

About

Provides JSON datatype support for the Eloquent ORM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 96.8%
  • Shell 3.2%