Skip to content
A simple and easy to use key-value database store for Laravel 5.5+
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Implement kvstore client facade Oct 1, 2019
.gitignore
LICENSE Initial commit Sep 29, 2019
README.md
composer.json

README.md

bgaze/laravel-kvstore

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.

Installation

Install the package using composer:

composer install 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.

To add or modify a value, use the set function:

// Insert some values:
KvStore::set('value1', 'a string');
KvStore::set('value2', '11111', 'integer');
KvStore::set('value3', ['test' => true], 'array');
KvStore::set('value4', \Carbon\Carbon::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);

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 passed to the function.

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

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

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

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

Finally, you can remove entries from the store using remove function:

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

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

Values casting

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

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

Cache managment.

All the store values are stored into database but managed using cache to avoid unecessary queries.

When the stored content is modified, or when Laravel application cache is cleared, the store cache is automatically regenerated.

Available functions

set:

Create or update a setting value.

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

get:

Get a setting value by key.

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

remove:

Delete a setting by key.

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

all:

Get all store content as a key-value collection.

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

refresh:

Purge cache to force a refresh on next get or all call.

/**
 * @return $this
 */
public static function refresh()
You can’t perform that action at this time.