softinklab/laravel-keyvalue-storage
| Install | |
|---|---|
composer require softinklab/laravel-keyvalue-storage |
|
| Latest Version: | v1.8 |
| PHP: | >=7.2 |
| License: | MIT |
| Last Updated: | Mar 27, 2022 |
| Links: | GitHub · Packagist |
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,
- spatie/valuestore by Spatie
- appstract/laravel-options by Appstract
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.
Related Packages
Admin Architect - Settings Module. Package provide easy way to load and store op...
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.8 | >=7.2 | MIT | ||
| 1.7 | ^7.3| | ^8.2 | MIT | |
| 1.5 | >=7.2.5 | ^6.0| | MIT | |
| 1.4 | ^7.2 | ^6.0| | MIT | |
| v1.3 | ^7.2 | ^6.0| | MIT | |
| v1.0 | ^7.2 | ^7.0 | MIT | |
| v0.1 | ^7.2 | ^7.0 | MIT |