|
| 1 | + |
| 2 | +[](https://packagist.org/packages/softinklab/laravel-keyvalue-storage) |
| 3 | +[](https://packagist.org/packages/softinklab/laravel-keyvalue-storage) |
| 4 | +[](LICENSE.md) |
| 5 | +[](https://travis-ci.org/SoftinkLab/laravel-keyvalue-storage) |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +You can install the package via composer: |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require softinklab/laravel-keyvalue-storage |
| 17 | +``` |
| 18 | + |
| 19 | +### Setup |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +In `config/app.php` add the following : |
| 24 | + |
| 25 | +1 - Service Provider to the providers array : |
| 26 | + |
| 27 | +```php |
| 28 | +SoftinkLab\LaravelKeyvalueStorage\KeyValueStorageServiceProvider::class, |
| 29 | +``` |
| 30 | + |
| 31 | +2 - Class alias to the aliases array : |
| 32 | + |
| 33 | +```php |
| 34 | +'KVOption' => SoftinkLab\LaravelKeyvalueStorage\Facades\KVOption::class, |
| 35 | +``` |
| 36 | + |
| 37 | +3 - Publish the config file |
| 38 | + |
| 39 | +```ssh |
| 40 | +php artisan vendor:publish --provider="SoftinkLab\LaravelKeyvalueStorage\KeyValueStorageServiceProvider" |
| 41 | +``` |
| 42 | + |
| 43 | +4 - Migrate database tables |
| 44 | + |
| 45 | +```ssh |
| 46 | +php artisan migrate |
| 47 | +``` |
| 48 | + |
| 49 | +### Configuration |
| 50 | + |
| 51 | +You can change the settings in `config/kvstorage.php`. |
| 52 | + |
| 53 | +Example : Databse Storage |
| 54 | +``` |
| 55 | +'method' => 'database', |
| 56 | +``` |
| 57 | + |
| 58 | +Example : File Storage |
| 59 | +``` |
| 60 | +'method' => 'file', |
| 61 | +'disk' => 'local', |
| 62 | +'path' => 'kvstorage/', |
| 63 | +``` |
| 64 | + |
| 65 | +## Usage |
| 66 | + |
| 67 | +### Using FACADE |
| 68 | + |
| 69 | +```php |
| 70 | +use SoftinkLab\LaravelKeyvalueStorage\Facades\KVOption; |
| 71 | + |
| 72 | +// Check is the option exists. Return true if found. |
| 73 | +KVOption::exists('key'); |
| 74 | + |
| 75 | +// Get the option value. |
| 76 | +KVOption::get('key'); |
| 77 | + |
| 78 | +// Get the option value. Default value is optional. If option not found default value is returned. |
| 79 | +KVOption::get('key', 'default value'); |
| 80 | + |
| 81 | +// Add new option. Comment is optional. Comment is only working in Database Mode. |
| 82 | +KVOption::set('key', 'value', 'comment'); |
| 83 | + |
| 84 | +// Add multiple options. |
| 85 | +// Example Input => [['key1', 'value1', 'comment1'],['key2', 'value2', 'comment2']] |
| 86 | +KVOption::setArray('array'); |
| 87 | + |
| 88 | +// Increment the value of a given key and return it as integer. Factor is used to determine the step. Default is one. |
| 89 | +KVOption::increment('key', 'factor'); |
| 90 | + |
| 91 | +// Decrement the value of a given key and return it as integer. Factor is used to determine the step. Default is one. |
| 92 | +KVOption::decrement('key', 'factor'); |
| 93 | + |
| 94 | +// Delete an option |
| 95 | +KVOption::remove('key'); |
| 96 | +``` |
| 97 | + |
| 98 | +### Using Helper |
| 99 | + |
| 100 | +```php |
| 101 | +// Check is the option exists. Return true if found. |
| 102 | +kvoption_exists('someKey'); |
| 103 | + |
| 104 | +// Get the option value. |
| 105 | +kvoption('key'); |
| 106 | + |
| 107 | +// Get the option value. Default value is optional. If option not found default value is returned. |
| 108 | +kvoption('key', 'default value'); |
| 109 | + |
| 110 | +// Add new option. Comment is optional. Comment is only working in Database Mode. |
| 111 | +kvoption(['key','value', 'comment']); |
| 112 | + |
| 113 | +// Add multiple options. |
| 114 | +// Example Input => [['key1', 'value1', 'comment1'],['key2', 'value2', 'comment2']] |
| 115 | +kvoption('array'); |
| 116 | +``` |
| 117 | +### Blade Templates |
| 118 | + |
| 119 | +You can use `kvoptions()` helper to access key-values in Blade Templates. |
| 120 | + |
| 121 | +```php |
| 122 | +{{kvoption('key')}} |
| 123 | +``` |
| 124 | + |
| 125 | +### Console |
| 126 | + |
| 127 | +There are some console commands to perform actions. |
| 128 | + |
| 129 | +**Create an Option** |
| 130 | +```bash |
| 131 | +php artisan kvoption:set {key} {value} --comment={comment} |
| 132 | +``` |
| 133 | +*comment is optional.* |
| 134 | + |
| 135 | +**Delete an Option** |
| 136 | +```bash |
| 137 | +php artisan kvoption:delete {key} |
| 138 | +``` |
| 139 | + |
| 140 | +## Contributing |
| 141 | + |
| 142 | +Contributions are welcome! |
| 143 | + |
| 144 | +## License |
| 145 | + |
| 146 | +Laravel Key Value Storage is open-sourced software licensed under the [MIT License](LICENSE.md). |
0 commit comments