Usage¶
Laravel Scoped Settings provides a fluent and expressive API to store, retrieve and organize configuration values at runtime.
You can access the settings manager using:
- the
setting()
helper (recommended) - or the
Setting::
facade
โ Basic usage¶
Set a global setting¶
setting()->set('site.name', 'My Laravel App');
Set a value with caching¶
// Cache the setting for 30 minutes
setting()->set('site.enabled', true, 1800);
// Use config-defined TTL (if set), or no cache if null
setting()->set('site.enabled', true);
Get a setting¶
$siteName = setting()->get('site.name'); // 'My Laravel App'
// If a TTL is configured, the value will be cached automatically.
Check if a setting exists¶
if (setting()->has('ui.theme')) {
// It was set
}
Provide a default fallback¶
$timezone = setting()->get('app.timezone', 'UTC');
Forget a setting¶
setting()->forget('site.name');
Deleting all settings for a model¶
setting()->for($user)->flush();
๐ฆ Value types¶
Values are automatically serialized to/from JSON. You can store:
- Strings
- Booleans
- Integers
- Arrays
setting()->set('notifications.enabled', true);
setting()->set('ui.theme', ['color' => 'blue', 'mode' => 'dark']);
$theme = setting()->get('ui.theme'); // ['color' => 'blue', 'mode' => 'dark']
๐ง Key format and grouping¶
You can group settings using dot notation:
setting()->set('seo.meta.title', 'Welcome!');
This will be stored as:
- group:
seo.meta
- key:
title
Or, if split:
setting()->set('seo.meta_title', 'Welcome!');
Will be stored as:
- group:
seo
- key:
meta_title
You can later retrieve an entire group:
setting()->group('seo');
// returns ['meta_title' => 'Welcome!']
๐งช Testing in your code¶
During testing you can easily assert settings:
expect(setting()->get('notifications.enabled'))->toBeTrue();