Skip to content

Laravel Key Value Storage using Database or JSON File

License

Notifications You must be signed in to change notification settings

SoftinkLab/laravel-keyvalue-storage

Repository files navigation

laravel keyvalue storage

Latest Version on Packagist Total Downloads Software License Build Status

Introduction

Laravel Key Value Storage is an easy and simple package to store key-value data globally in Laravel. This package supports both Database and JSON File as storage methods. This package also comes with helper which simplify your key-value storage access in Code as well as in Blade Template.

Installation

You can install the package via composer:

composer require softinklab/laravel-keyvalue-storage

Setup

This package supports the auto-discovery feature of Laravel 5.5 and above, So skip these Setup instructions if you're using Laravel 5.5 and above.

In config/app.php add the following :

1 - Service Provider to the providers array :

SoftinkLab\LaravelKeyvalueStorage\KeyValueStorageServiceProvider::class,

2 - Class alias to the aliases array :

'KVOption' => SoftinkLab\LaravelKeyvalueStorage\Facades\KVOption::class,

3 - Publish the config file

php artisan vendor:publish --provider="SoftinkLab\LaravelKeyvalueStorage\KeyValueStorageServiceProvider"

4 - Migrate database tables

php artisan migrate

Configuration

You can change the settings in config/kvstorage.php.

Example : Databse Storage

'method' => 'database',
'table_name' => 'kv_storage',

Example : File Storage

'method' => 'file',
'disk' => 'local',
'path' => 'kvstorage/',

Usage

Using FACADE

use SoftinkLab\LaravelKeyvalueStorage\Facades\KVOption;

// Check is the option exists. Return true if found.
KVOption::exists('key');

// Get the option value.
KVOption::get('key');

// Get the option value. Default value is optional. If option not found default value is returned.
KVOption::get('key', 'default value');

// Add new option. Comment is optional. Comment is only working in Database Mode.
KVOption::set('key', 'value', 'comment');

// Add multiple options.
// Example Input => [['key1', 'value1', 'comment1'],['key2', 'value2', 'comment2']]
KVOption::setArray('array');

// Increment the value of a given key and return it as integer. Factor is used to determine the step. Default is one.
KVOption::increment('key', 'factor');

// Decrement the value of a given key and return it as integer. Factor is used to determine the step. Default is one.
KVOption::decrement('key', 'factor');

// Delete an option
KVOption::remove('key');

Using Helper

// Check is the option exists. Return true if found.
kvoption_exists('someKey');

// Get the option value.
kvoption('key');

// Get the option value. Default value is optional. If option not found default value is returned.
kvoption('key', 'default value');

// Add new option. Comment is optional. Comment is only working in Database Mode.
kvoption(['key','value', 'comment']);

// Add multiple options.
// Example Input => [['key1', 'value1', 'comment1'],['key2', 'value2', 'comment2']]
kvoption('array');

Blade Templates

You can use kvoptions() helper to access key-values in Blade Templates.

{{kvoption('key')}}

Console

There are some console commands to perform actions.

Create an Option

php artisan kvoption:create {key} {value} --comment={comment}

comment is optional.

Delete an Option

php artisan kvoption:delete {key}

Credits

This package is inspired by,

Contributing

Contributions are welcome! Please refer CONTRIBUTING for details.

License

Laravel Key Value Storage is open-sourced software licensed under the Apache 2.0 License.