<commit_before># Dialect

[![Build Status](https://travis-ci.org/darrylkuhn/dialect.svg?branch=master)](https://travis-ci.org/darrylkuhn/dialect) [![Code Climate](https://codeclimate.com/github/darrylkuhn/dialect/badges/gpa.svg)](https://codeclimate.com/github/darrylkuhn/dialect) [![Test Coverage](https://codeclimate.com/github/darrylkuhn/dialect/badges/coverage.svg)](https://codeclimate.com/github/darrylkuhn/dialect)

Dialect provides JSON datatype support for the [Eloquent ORM](http://laravel.com/docs/eloquent).

## 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:

```php
class Photo extends Eloquent
{
    use Eloquent\Dialect\Json;
    protected $jsonColumns = ['json_data'];
}
```
And then this:
```php
$attr = json_decode($photo->json_data);
$attr->key = $value;
$photo->json_data = json_encode($attr);
```
becomes this:
```php
$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:
```php
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:
```php
$referral->setJsonAttribute( 'additional_details', 'fizz', 'buzz' );
```
At this point this implementation is pretty bare bones and only supports PostgreSQL. There are lots of opportunities to enhance and improve. If you're interested in contributing please submit merge/pull requests.<commit_msg>Updated readme based on feedback from github user that0n3guy<commit_after># Dialect

[![Build Status](https://travis-ci.org/darrylkuhn/dialect.svg?branch=master)](https://travis-ci.org/darrylkuhn/dialect) [![Code Climate](https://codeclimate.com/github/darrylkuhn/dialect/badges/gpa.svg)](https://codeclimate.com/github/darrylkuhn/dialect) [![Test Coverage](https://codeclimate.com/github/darrylkuhn/dialect/badges/coverage.svg)](https://codeclimate.com/github/darrylkuhn/dialect)

Dialect provides JSON datatype support for the [Eloquent ORM](http://laravel.com/docs/eloquent). 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:

```php
class Photo extends Eloquent
{
    use Eloquent\Dialect\Json;
    protected $jsonColumns = ['json_data'];
}
```
And then this:
```php
$attr = json_decode($photo->json_data);
$attr->key = $value;
$photo->json_data = json_encode($attr);
```
becomes this:
```php
$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):
```php
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:
```php
$referral->setJsonAttribute( 'additional_details', 'fizz', 'buzz' );
```
{'fraction_non_alphanumeric': 0.10494203782794387, 'fraction_numerical': 0.0004067520846044336, 'mean_word_length': 5.109316770186336, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4165862', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1396, 'n_words': 575}