Skip to main content

Value Store

The ValueStore model provides a database-backed key-value storage system for application settings and configuration data. This replaces filesystem-based storage solutions and ensures compatibility with cloud environments where filesystem access is limited.

Purpose

Many applications need to store dynamic configuration values, user preferences, or temporary settings that cannot be defined in static configuration files. The ValueStore provides a simple, database-backed solution that works reliably across all deployment environments, including Laravel Cloud.

Usage

The model provides convenient static methods for managing key-value pairs:

Store Values

ValueStore::put('app_maintenance_mode', true);
ValueStore::set('last_sync_timestamp', now());
ValueStore::put('user_preferences', [
    'theme' => 'dark',
    'language' => 'en',
    'notifications' => true
]);

Retrieve Values

$maintenanceMode = ValueStore::get('app_maintenance_mode', false);
$lastSync = ValueStore::get('last_sync_timestamp');
$preferences = ValueStore::get('user_preferences', []);

Check if Keys Exist

$hasPreferences = ValueStore::exists('user_preferences');
// Returns: true or false

Remove Values

ValueStore::forget('temporary_setting');

Data Types

The value column uses JSON casting, which means you can store:
  • Strings: ValueStore::put('name', 'John')
  • Numbers: ValueStore::put('count', 42)
  • Booleans: ValueStore::put('enabled', true)
  • Arrays: ValueStore::put('tags', ['php', 'laravel'])
  • Objects: ValueStore::put('config', ['host' => 'localhost', 'port' => 5432])

Migration from Spatie Valuestore

If you’re migrating from Spatie’s filesystem-based Valuestore, the API is largely compatible:
// Old (Spatie Valuestore)
$store = Valuestore::make(storage_path('settings.json'));
$store->put('key', 'value');
$value = $store->get('key', 'default');

// New (Database ValueStore)
ValueStore::put('key', 'value');
$value = ValueStore::get('key', 'default');

Benefits

  • Cloud Compatible: Works in environments without persistent filesystem access
  • Scalable: Database-backed storage scales with your application
  • Atomic: Database transactions ensure data consistency
  • Searchable: Can query and filter stored values using Eloquent
  • Backup Friendly: Included in regular database backups