igaster / laravel-model-options by igaster

Store an Options array in a JSON column. Get/Set values as if they were seperate keys in the Database
713
11
3
Package Data
Maintainer Username: igaster
Maintainer Contact: igasteratos@gmail.com (Giannis Gasteratos)
Package Create Date: 2015-12-16
Package Last Update: 2020-10-11
Language: PHP
License: MIT
Last Refreshed: 2024-04-22 03:09:18
Package Statistics
Total Downloads: 713
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 11
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

Description

Laravel License Build Status Downloads

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

  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:

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

     use \igaster\modelOptions\modelOptions;
    
  2. 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:

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

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

//--- 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);
}