Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
igaster committed Dec 16, 2015
1 parent 18d44c3 commit a814def
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Description
Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

A simple Trait to store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

## Installation

Expand All @@ -16,20 +17,20 @@ and install with `composer update`
1. Define a JSON key with name 'options' in your migration file:


$table->json('options');
$table->json('options');


2. Use the Trait in the coresponding model:

use igaster\modelOptions\modelOptions;
use igaster\modelOptions\modelOptions;

3. Define the valid option keys in model:

protected $validOptions=[
'option_1',
'option_2',
];
protected $validOptions=[
'option_1',
'option_2',
];

4. Access option key as if they were columns in your Database. eg:

$model->option_1 = 'value1';
$model->option_1 = 'value1';
7 changes: 4 additions & 3 deletions src/modelOptions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php namespace igaster\modelOptions;



/**********************************
1.Create a column in migrations:
Expand All @@ -24,18 +22,20 @@

trait modelOptions {


// Laravel mutators: get options
public function getOptionsAttribute($value){
if ($value == null)
return [];

return json_decode($value, true);
}

// Laravel mutators: set options
public function setOptionsAttribute($value){
$this->attributes['options'] = json_encode($value);
}

// Return valid keys from options array
public function __get($key) {
if (in_array($key, $this->validOptions))
if(array_key_exists($key, $this->options))
Expand All @@ -46,6 +46,7 @@ public function __get($key) {
return parent::__get($key);
}

// Set valid keys in options array
public function __set($key, $value) {
if (in_array($key, $this->validOptions)) {
$options = $this->options;
Expand Down

0 comments on commit a814def

Please sign in to comment.