Download the PHP package igaster/laravel-model-options without Composer
On this page you can find all versions of the php package igaster/laravel-model-options. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download igaster/laravel-model-options
More information about igaster/laravel-model-options
Files in igaster/laravel-model-options
Package laravel-model-options
Short Description Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database
License MIT
Homepage https://github.com/igaster/laravel-model-options.git
Informations about the package laravel-model-options
Description
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
Edit your project's composer.json
file to require:
"require": {
"igaster/laravel-model-options": "~1.0"
}
and install with composer update
How to use
- Define a JSON key with name 'options' in your migration file:
note that older versions of mySql don't support the json type. In that case you can safely fallback to a string type:
-
Use the Trait in the coresponding model:
use \igaster\modelOptions\modelOptions;
-
Define the valid option keys in model:
protected $validOptions=[ 'option_1', 'option_2', ];
Usage:
Access option key as if they were columns in your Database. eg:
$model->option_1 = 'value1';
Handle Conflicts:
This Trait makes use of the __get()
and __set()
magic methods to perform its ... well... magic! However if you want to implement these functions in your model or another trait then php will complain about conflicts. To overcome this problem you have to hide the Traits methods when you import it:
and call them manually from your __get()
/ __set()
mehods: