- How do I install spatie/laravel-valuestore in my Laravel project?
- Run `composer require spatie/laravel-valuestore` in your project directory. No additional configuration is needed—it integrates seamlessly with Laravel’s filesystem. The default JSON file is stored at `storage/app/valuestore.json`.
- What Laravel versions does this package support?
- spatie/laravel-valuestore supports Laravel 8.x, 9.x, and 10.x. Check the [GitHub repository](https://github.com/spatie/valuestore) for the latest compatibility details before upgrading.
- Can I use this for production feature flags or app configurations?
- Yes, but ensure file permissions restrict access to the JSON file (e.g., `storage/app` should be writable only by the web server user). For sensitive data, encrypt values using Laravel’s built-in encryption (e.g., `config/encryption.php`).
- How do I handle concurrent writes to the JSON file in a multi-server environment?
- Use Laravel’s `Storage::lock()` to prevent race conditions. For distributed setups, consider a shared database or caching layer (e.g., Redis) instead, as JSON file storage isn’t designed for concurrent writes across servers.
- What’s the difference between `put()` and `put(['key1' => 'value1'])`?
- Both methods store key-value pairs, but the array syntax (`put(['key1' => 'value1'])`) lets you write multiple keys in a single operation. This is useful for bulk updates, like initializing a store with default settings.
- How do I cache the JSON file for better performance?
- Wrap `Valuestore::all()` or `Valuestore::get()` calls in Laravel’s cache (e.g., `Cache::remember('valuestore', 60, fn() => Valuestore::all())`). This reduces file I/O but requires manual cache invalidation when data changes.
- Is there a way to use a database instead of a JSON file?
- No, this package is designed for JSON file storage only. For database-backed settings, use alternatives like `spatie/laravel-settings`, which supports MySQL, PostgreSQL, and SQLite.
- How do I test Valuestore in PHPUnit?
- Mock the `Valuestore` interface or use Laravel’s `Storage` facade to simulate file operations. Test edge cases like concurrent writes by temporarily disabling file locking in tests.
- What happens if the JSON file gets corrupted?
- The package will throw an exception if the file is unreadable. To mitigate corruption, ensure atomic writes (e.g., write to a temp file and rename it) and validate JSON structure on read.
- Can I use Valuestore for user-specific settings instead of sessions?
- Yes, but avoid storing large or sensitive data. For scalability, consider Laravel’s built-in session driver or a dedicated database table. Valuestore is best for small, app-wide configurations.