Laravel Kvstore

GitHub license Maintenance GitHub release (latest by date) GitHub stars Packagist

A simple and easy to use key-value database store for Laravel 5.5+


All values are stored into database and managed using cache to avoid unecessary queries.
Casting is supported to manage values type.

Any contribution or feedback is highly welcomed, please feel free to create a pull request or submit a new issue.

Installation

Install the package using composer:

composer require bgaze/laravel-kvstore

Publish the required migration:

php artisan vendor:publish --tag=kvstore

Then create the table:

php artisan migrate

Usage

  • The KvStore facade offers static methods to manage the store content.
  • All the store values are stored into database but managed using cache to avoid unnecessary queries.
  • When the stored content is modified, or when the application cache is cleared, the store cache is automatically regenerated.

Add / edit entry

To create or update an entry, use the set function:

/**
 * @param string $key
 * @param mixed $value
 * @param string|null $type
 * @return $this
 */
public static function set($key, $value, $type = null)

The $type argument allows to automatically cast the entry value.

Supported cast types are: integer, real, float, double, decimal:<digits>, string, boolean, object, array, collection, date, datetime, timestamp.
When casting to decimal, you must define the number of digits (decimal:2).

This feature uses in background Eloquent Model's Attribute Casting, please see documentation for more details.

Examples:

// Insert some values:
KvStore::set('value1', 'a string');
KvStore::set('value2', '11111', 'integer');
KvStore::set('value3', ['test' => true], 'array');
KvStore::set('value4', now(), 'datetime');

// Update value keeping cast type:
KvStore::set('value3', ['test' => false]);

// Update value changing cast type:
KvStore::set('value3', 22222, 'integer');

// Update value removing cast type.
KvStore::set('value3', 22222, false);

Retrieve entries

Use the get function to retrieve entries from the store.
If the key doesn't exists, null will be returned unless a default value is specified.

/**
 * @param  string  $key
 * @param  mixed  $default
 * @return mixed
 */
public static function get($key, $default = null)

You can also get all the store content as a collection using the all function:

/**
 * @return \Illuminate\Support\Collection
 */
public static function all()

Examples:

// Get value by key:
$value1 = KvStore::get('value1');

// Get value by key, passing a default value:
$value2 = KvStore::get('value2', 'default value');

// Get all the store content and convert it to array:
$store = KvStore::all()->toArray();

Remove entries

Entries can be removed from the store using remove function:

/**
 * @param  string|array  $keys
 * @return $this
 */
public static function remove($keys)

Examples:

// Remove an entry by key:
KvStore::remove('value1');

// Remove multiple entries by key:
KvStore::remove(['value1', 'value2']);

Force cache refresh

Use the refresh function to force a cache refresh on next get or all call.

/**
 * @return $this
 */
public static function refresh()

Packages

Feel free to visit my other packages:

Bootstrap 4 forms builder for Laravel 5.8+

This package uses in background Laravel Collective HTML to simplify Bootstrap 4 forms creation into Laravel applications.
Model form binding and automatic error display are supported, as well as most of Bootstrap forms features : form layouts, custom fields, input groups, ...

Github Documentation

PHP-CS-Fixer for Laravel 5.5+

This package allows to use PHP-CS-Fixer right into Laravel 5.5+ applications to format PHP code.

Github Documentation

Bootstrap 4 dialogs

BSD is a tiny and flexible collection of dialog popups based on Bootstrap 4 modals.
Custom dialogs can be easily defined, in addition to built-in ones (alert, confirm and prompt).

Github Documentation

Bootstrap Color Palette

BCP is a simple color palette for Bootstrap 4, like in Google doc, built on the top of Bootstrap 4 Popover plugin.

Github Documentation

Php DotEnv

A simple and standalone DotEnv parser for PHP 5.6+

Github Documentation

Blade Indenter

A very simple formatter for Laravel 5.8+ Blade templates

Github Documentation