A Laravel 12/13 compatible package to store custom settings in the database and cache, with auto-discovery, publishable migrations & models, and group-based organization.
| Package version | Laravel | PHP |
|---|---|---|
^2.0 |
12, 13 | 8.2+ |
^1.0 |
12 | 8.1+ |
composer require webafra/larasettingNote: Since this package supports Laravel auto-discovery, you do not need to manually register the ServiceProvider or Facade. If you prefer to register manually, add the following to
config/app.php:
'providers' => [
Webafra\LaraSetting\LaraSettingServiceProvider::class,
],
'aliases' => [
'Setting' => Webafra\LaraSetting\Facade\Setting::class,
],# Publish only migrations
php artisan vendor:publish --tag=migrations
# Publish only models
php artisan vendor:publish --tag=models
# Publish both
php artisan vendor:publish --tag=allThen run the migration:
php artisan migrateuse Webafra\LaraSetting\Facade\Setting;
// Store a value
Setting::set('site_name', 'My Website');
// Retrieve a value (with optional default)
$name = Setting::get('site_name', 'Default Name');
// Check if a setting exists
if (Setting::has('site_name')) {
// ...
}
// Delete a setting
Setting::delete('site_name');Settings can be organized into named groups (default group is general):
// Store in a specific group
Setting::set('color', '#ffffff', false, 'theme');
Setting::set('font', 'Inter', false, 'theme');
// Retrieve from a group
$color = Setting::get('color', '#000000', 'theme');
// Get all settings in a group as key-value array
$theme = Setting::getGroup('theme');
// ['color' => '#ffffff', 'font' => 'Inter']Primary settings are a special subset (e.g. site-wide configuration) that can be loaded all at once:
// Mark as primary when storing
Setting::set('app_name', 'My App', true);
// Store multiple primaries (arrays are auto JSON-encoded)
Setting::storePrimary([
'app_name' => 'My App',
'social_links' => ['twitter' => 'https://...'],
]);
// Retrieve all primary settings as key-value array
$primaries = Setting::getPrimary();// Store multiple settings at once (optionally in a group)
Setting::store([
'key1' => 'value1',
'key2' => 'value2',
], 'my_group');All values are cached indefinitely via Cache::forever. To invalidate all cached settings:
Setting::clean();Setting::set(string $key, mixed $value, bool $is_primary = false, string $group = 'general'): mixedStores or updates a setting. Arrays are automatically JSON-encoded. Returns the stored value.
| Parameter | Type | Default | Description |
|---|---|---|---|
$key |
string |
— | Setting key |
$value |
mixed |
— | Value to store (arrays are JSON-encoded automatically) |
$is_primary |
bool |
false |
Mark as a primary setting |
$group |
string |
'general' |
Group namespace |
Setting::get(string $key, mixed $default = null, string $group = 'general'): mixedRetrieves a setting value. Result is cached indefinitely.
| Parameter | Type | Default | Description |
|---|---|---|---|
$key |
string |
— | Setting key |
$default |
mixed |
null |
Fallback value if key does not exist |
$group |
string |
'general' |
Group namespace |
Setting::has(string $key, string $group = 'general'): boolReturns true if the setting exists in the database (does not hit cache).
Setting::delete(string $key, string $group = 'general'): boolDeletes a setting from the database and clears its cache entry. Returns true on success.
Setting::store(array $settings, string $group = 'general'): intStores multiple settings at once. Returns the number of settings saved.
Setting::store([
'key1' => 'value1',
'key2' => ['a', 'b'], // arrays are auto JSON-encoded
], 'my_group');Setting::storePrimary(array $settings, string $group = 'general'): intSame as store() but marks every entry as primary (is_primary = true). Returns the number of settings saved.
Setting::getPrimary(mixed $default = null): mixedReturns all primary settings across all groups as a flat ['key' => 'value'] array. Result is cached indefinitely.
Setting::getGroup(string $group): arrayReturns all settings within a group as a ['key' => 'value'] array. Result is cached indefinitely.
Setting::clean(): voidClears the cache for every setting, every group, and the primary collection. Does not delete database records.
| Method | DB read | DB write | Cache read | Cache write | Returns |
|---|---|---|---|---|---|
set() |
— | ✓ | — | ✓ | mixed |
get() |
on miss | — | ✓ | on miss | mixed |
has() |
✓ | — | — | — | bool |
delete() |
— | ✓ | — | clears | bool |
store() |
— | ✓ | — | ✓ | int |
storePrimary() |
— | ✓ | — | ✓ | int |
getPrimary() |
on miss | — | ✓ | on miss | array |
getGroup() |
on miss | — | ✓ | on miss | array |
clean() |
— | — | — | clears all | void |
Version 2.0 introduced a group column and a composite unique key on (group, key). After upgrading the package, publish and run a new migration:
php artisan vendor:publish --tag=migrations
php artisan migrateExisting rows will receive the default group value
general, so all existingSetting::get()andSetting::set()calls remain fully compatible without any code changes.
MIT