Skip to main content
Back to packages
769 GitHub stars

spatie/valuestore

View on GitHub

Description

Easily store some values

This package makes it easy to store and retrieve some loose values. Stored values are saved as a json file.

It can be used like this:

use Spatie\Valuestore\Valuestore;
 
$valuestore = Valuestore::make($pathToFile);
 
$valuestore->put('key', 'value');
 
$valuestore->get('key'); // Returns 'value'
 
$valuestore->has('key'); // Returns true
 
// Specify a default value for when the specified key does not exist
$valuestore->get('non existing key', 'default') // Returns 'default'
 
$valuestore->put('anotherKey', 'anotherValue');
 
// Put multiple items in one go
$valuestore->put(['ringo' => 'drums', 'paul' => 'bass']);
 
$valuestore->all(); // Returns an array with all items
 
$valuestore->forget('key'); // Removes the item
 
$valuestore->flush(); // Empty the entire valuestore
 
$valuestore->flushStartingWith('somekey'); // remove all items whose keys start with "somekey"
 
$valuestore->increment('number'); // $valuestore->get('number') will return 1
$valuestore->increment('number'); // $valuestore->get('number') will return 2
$valuestore->increment('number', 3); // $valuestore->get('number') will return 5
 
// Valuestore implements ArrayAccess
$valuestore['key'] = 'value';
$valuestore['key']; // Returns 'value'
isset($valuestore['key']); // Return true
unset($valuestore['key']); // Equivalent to removing the value
 
// Valuestore implements Countable
count($valuestore); // Returns 0
$valuestore->put('key', 'value');
count($valuestore); // Returns 1

Related Content on Laravel Daily

Video

Recent Courses on Laravel Daily

Next.js Basics for Laravel Developers

11 lessons
58 min

Roles and Permissions in Laravel 13

14 lessons
57 min

How to Build Laravel 13 API From Scratch

30 lessons
1 h 23 min