fomvasss/laravel-variables

Dynamic management of variables/configs in Laravel app

5.3.0 2024-03-20 17:13 UTC

README

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31

Laravel Variables

Latest Stable Version Total Downloads Latest Unstable Version License Quality Score

Dynamic management of variables/configs in Laravel app: creating and updating they in database, using cache and artisan commands, replace default Laravel configs, etc.

Installation

Run:

composer require fomvasss/laravel-variables

Publish the config, migration:

php artisan vendor:publish --provider="Fomvasss\Variable\VariableServiceProvider"

Run migrate:

php artisan migrate

Usage

Facade Variable

<?php
Variable::all();
Variable::get('var_key');
Variable::save('app_name', 'My Var');

Use groupped (multilanguages) variables:

<?php
Variable::setGroup('en')->all(); // return Collection!
Variable::setGroup('uk')->get('var_key');
Variable::get('var_key', null, 'en'); 
Variable::get('en|var_key');
Variable::save('uk|var_key', 'UK var');

Use array (json) variables:

Variable::saveArray('links', ['https::google.com', 'https://laravel.com']);   // save PHP array
Variable::getArray('links');    // return default PHP array!

Use cache variables:

Variable::setGroup('uk')->save('app_name', 'Blog');
Variable::setGroup('uk')->useCache(false)->get('app_name');
//or
Variable::get('var_key', null, 'uk', false);

Helpers

variable($name, $default = null, $group = null);

Replace Laravel configs with variables

Set in config/variables.php option config_key_for_vars=vars

Add keys in variable_config array: variable_key => config_key

Console command

variable:all            # Show all variables
variable:get            # Get single variable
variable:save           # Save single variable
variable:cache-clear    # Cache clear all variables

Use cache

Set in config/variables.php option cache.time seconds for cache.

Clear variable cache with console:

php artisan variable:cache-clear

or

php artisan cache:forget laravel.variables.cache

Clear variable cache in controller after update var:

Variable::cacheClear();
//or
\Cache::forget('laravel.variables.cache');