2017 © Pedro Peláez
 

library laravel-model-options

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

image

igaster/laravel-model-options

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

  • Saturday, January 2, 2016
  • by igaster
  • Repository
  • 1 Watchers
  • 7 Stars
  • 639 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 14 % Grown

The README.md

Description

Laravel License Build Status Downloads, (*1)

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

Installation

Edit your project's composer.json file to require:, (*3)

"require": {
    "igaster/laravel-model-options": "~1.0"
}

and install with composer update, (*4)

How to use

  1. Define a JSON key with name 'options' in your migration file:
$table->json('options')->nullable();

note that older versions of mySql don't support the json type. In that case you can safely fallback to a string type:, (*5)

$table->string('options')->nullable();
  1. Use the Trait in the coresponding model:, (*6)

    use \igaster\modelOptions\modelOptions;
  2. Define the valid option keys in model:, (*7)

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

Usage:

Access option key as if they were columns in your Database. eg:, (*8)

    $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:, (*9)

use igaster\modelOptions\modelOptions {
    __get as private; 
    __set as private; 
}

and call them manually from your __get() / __set() mehods:, (*10)

//--- copy these in your model if you need to implement __get() __set() methods

public function __get($key) {
    // Handle modelOptions keys
    $result=$this->modelOptions_get($key);
    if ($this->modelOptions_handled)
        return $result;

    //your code goes here

    return parent::__get($key);
}

public function __set($key, $value) {
    // Handle modelOptions keys
    $this->modelOptions_set($key, $value);
    if ($this->modelOptions_handled)
        return;

    //your code goes here

    parent::__set($key, $value);
}     

The Versions

02/01 2016

dev-master

9999999-dev https://github.com/igaster/laravel-model-options.git

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

  Sources   Download

MIT

The Requires

 

The Development Requires

by Giannis Gasteratos

trait package laravel-5.x model-options model-settings options-json settings-json

24/12 2015

v1.0.1

1.0.1.0 https://github.com/igaster/laravel-model-options.git

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

  Sources   Download

MIT

The Requires

 

by Giannis Gasteratos

trait package laravel-5.x model-options model-settings options-json settings-json

16/12 2015

v1.0

1.0.0.0 https://github.com/igaster/laravel-model-options.git

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

  Sources   Download

MIT

The Requires

 

by Giannis Gasteratos

trait package laravel-5.x model-options model-settings options-json settings-json

16/12 2015

v0.1

0.1.0.0 https://github.com/igaster/laravel-theme

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database

  Sources   Download

MIT

The Requires

 

by Giannis Gasteratos

trait package laravel-5.x model-options model-settings options-json settings-json