Basic Usage
Basic Usage
- Introduction
- Setting a value
- Retrieving a value
- Check if a setting exists
- Remove a setting from storage
- Boolean settings
- Retrieve all settings
- Flushing settings
Introduction
You can interact with settings via the Settings
facade, or by using the settings()
helper function, which returns an instance of Rawilk\Settings\Settings
.
Setting a value
// Create a new setting
Settings::set('foo', 'bar');
// Update an existing setting
Settings::set('foo', 'updated value');
Retrieving a value
Settings::get('foo');
// Retrieve a non-persisted setting
Settings::get('not persisted', 'my default'); // 'my default'
{note} By default, the default value passed into
get()
will be cached. This may not always be desirable, and can be disabled by setting thecache_default_value
config option tofalse
in theconfig/settings.php
file.
Check if a setting exists
Settings::has('foo');
Remove a setting from storage
Settings::forget('foo');
Boolean settings
Settings::set('app.debug', true);
Settings::isTrue('app.debug'); // true
Settings::isFalse('app.debug'); // false
Settings::set('app.debug', false);
Settings::isFalse('app.debug'); // true
Retrieve all settings
Using all
, you can retrieve all the stored settings, which will be returned as a collection. You may also retrieve a subset of settings
by passing in an array of keys to retrieve.
{note} Either the
ReadableKeyGenerator
or a custom key generator must be configured to be used before this method can be used. See KeyGenerator docs for more information.In a future major release, the
ReadableKeyGenerator
may be set to be the default key generator.
Settings::all();
// Subset of settings
Settings::all(['foo', 'bar']);
The collection of settings returned from this method will contain objects structured like this:
{
"id": 1,
"key": "foo",
"value": "bar",
"original_key": "foo"
}
{tip} The
original_key
property is set by settings to reflect the key that is used in the database.
{tip} If you'd like to retrieve all settings that do not have a context, you may provide a
false
value for a context:settings()->context(false)->all()
Flushing settings
Multiple settings can be deleted at one time using the flush
method on settings. It works similar to forget
, however
it is not able to flush the cache for each setting, unless you pass in a subset of keys to flush.
Settings::flush();
// Flush a subset of settings
Settings::flush(['foo', 'bar']);