laravel-settings

API

Settings

The methods shown here are available on the Rawilk\Settings\Settings service class. You can call any of the methods via either the Settings Facade or the settings() helper function. Several methods return an instance of Settings, so they are chainable to each other.

Remove a persisted setting from storage.

public function forget(string|UnitEnum $key): mixed

Retrieve a setting from storage.

public function get(
    string|UnitEnum $key,
    mixed $default = null,
    null|int|Closure|DateTimeInterface|DateInterval|array $cacheTtl = null,
): mixed 

Determine if a setting has been persisted to storage.

public function has(string|UnitEnum $key): bool

{note} Any caching used to store settings is not checked with has(). It will always perform a database query when using database-based drivers.

Create or update a setting and persist to storage.

public function set(
    string|UnitEnum $key,
    mixed $value = null,
    null|int|Closure|DateTimeInterface|DateInterval|array $cacheTtl = null,
): mixed

Determine if a setting is set to a falsy value. Returns true if the value is false, 0, or '0'.

public function isFalse(
    string|UnitEnum $key,
    mixed $default = false,
    null|int|Closure|DateTimeInterface|DateInterval|array $cacheTtl = null,
): bool

Determine if a setting is set to a truthy value. Returns true if the value is true, 1, or '1'.

public function isTrue(
    string|UnitEnum $key,
    mixed $default = true,
    null|int|Closure|DateTimeInterface|DateInterval|array $cacheTtl = null,
): bool

Retrieve all stored settings, or a subset of settings with a $keys parameter.

use Illuminate\Support\Collection;

/**
 * @param array|string|null $keys Only return a subset of settings.
 * @return Collection<int, object>
 */
public function all($keys = null): Collection

{note} The KeyValueContextSerializer context serializer must be used for this to work properly with subsets of keys.

Flush all settings or a subset of settings from storage.

/**
 * @param array|string|null $keys Only flush a subset of settings.
 */
public function flush($keys = null): mixed

Set the context for the current operation. Omit or set $context to null to remove context. Note: The context set here is only applied to a single settings call or settings instance.

use Rawilk\Settings\Support\Context;

public function context(Context|bool|null $context = null): static

Scope the context of settings within a callback function.

use Rawilk\Settings\Support\Context;

public function withContext(Context|bool|null $context, Closure $callback): mixed

Note: You will need to use the same settings instance within the callback to ensure the context is applied correctly. We provide the instance of the settings service to your callback:

use Rawilk\Settings\Facades\Settings;
use Rawilk\Settings\Settings as SettingsService;
use Rawilk\Settings\Support\Context;

Settings::withContext(new Context(['foo' => 'bar']), function (SettingsService $settings) {
    $settings->set('foo', 'bar');
});

Get the correct cache key for a given setting.

public function cacheKeyForSetting(string|UnitEnum $key): string

Enable caching for settings operations.

public function enableCache(): static

{note} This persists between settings calls.

Disable caching for settings operations.

public function disableCache(): static

{note} This persists between settings calls.

Scope operations to disable the cache within a callback.

public function withoutCache(Closure $callback): mixed

Set the TTL for items cached by settings.

public function cacheItemsFor(null|int|array|Closure|DateTimeInterface|DateInterval $ttl): static

{note} This persists between settings calls.

Define a cache prefix for setting keys on the fly. Note: this only lasts for a single settings call or settings instance.

public function prefixCacheWith(string $prefix): static

Define a cache store to use for settings on the fly. Note: this only lasts for a single settings call or settings instance.

public function useCacheStore(null|string|UnitEnum $store = null): static

Disable teams completely.

public function disableTeams(): static

{note} This persists between settings calls.

Enable the teams feature.

public function enableTeams(): static

{note} This persists between settings calls.

Scope operations to a specific team. Note: This only applies if teams are enabled.

public function usingTeam(mixed $team, ?Closure $callback = null): mixed

Scope operations to no team (team_id = null). Note: This is only applies if teams are enabled.

public function noTeam(?Closure $callback = null): mixed

Enable value encryption for settings.

public function enableEncryption(): static

{note} This persists between settings calls.

Prevent settings from encrypting values.

public function disableEncryption(): static

{note} This persists between settings calls.

Disable encryption for setting values within a callback.

public function withoutEncryption(Closure $callback): mixed

Register a custom driver for settings.

public function extend(string $driver, Closure $callback): static

Use a specific driver for a single callback.

public function usingDriver(Driver|string|UnitEnum $driver, Closure $callback): mixed
Previous
Enums