diff --git a/README.md b/README.md index 4ab746c..dfdb911 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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'; diff --git a/src/modelOptions.php b/src/modelOptions.php index 765109a..ab5e6f9 100644 --- a/src/modelOptions.php +++ b/src/modelOptions.php @@ -1,7 +1,5 @@ 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)) @@ -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;