mangoldsecurity / settings
Application wide settings stored in your database
Fund package maintenance!
Outerweb
Installs: 33
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 8
pkg:composer/mangoldsecurity/settings
Requires
- php: ^8.2
- illuminate/contracts: ^11.0||^12.0
- illuminate/database: ^11.0||^12.0
- illuminate/support: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
Settings
This package provides application wide settings stored in your database
Table of Contents
Installation
You can install the package via composer:
composer require mangoldsecurity/settings
Run the install command to publish the migration and the config file:
php artisan settings:install
Usage
This package tries to mimic the Config functionality of Laravel as close as possible.
You can use the MangoldSecurity\Settings\Facades\Setting facade or the setting() helper function to work with your stored settings.
Storing settings
Storing a single setting
use MangoldSecurity\Settings\Facades\Setting; Setting::set('key', 'value'); // or setting(['key' => 'value']); // or setting()->set('key', 'value');
Storing multiple settings
use MangoldSecurity\Settings\Facades\Setting; Setting::set([ 'key1' => 'value1', 'key2' => 'value2', ]); // or setting([ 'key1' => 'value1', 'key2' => 'value2', ]); // or setting()->set([ 'key1' => 'value1', 'key2' => 'value2', ]);
Storing nested settings
You can use "dot" notation to store nested settings:
use MangoldSecurity\Settings\Facades\Setting; Setting::set('parent.child', 'value'); // or setting(['parent.child' => 'value']); // or setting()->set('parent.child', 'value');
This will store a single table entry with the key parent.child and the value value.
You can also store nested settings using arrays:
use MangoldSecurity\Settings\Facades\Setting; Setting::set([ 'parent' => [ 'child1' => 'value', 'child2' => 'value', ], ]); // or setting([ 'parent' => [ 'child1' => 'value', 'child2' => 'value', ], ]); // or setting()->set([ 'parent' => [ 'child1' => 'value', 'child2' => 'value', ], ]);
This will store two table entries:
parent.child1with the valuevalueparent.child2with the valuevalue
Retrieving settings
Retrieving a single setting
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::get('key'); // or $value = setting('key'); // or $value = setting()->get('key');
This will return the value of the setting stored with the key key. If the setting does not exist, it will return null.
You can also provide a default value that will be returned if the setting does not exist:
$value = Setting::get('key', 'default_value'); // or $value = setting('key', 'default_value'); // or $value = setting()->get('key', 'default_value');
This will return default_value if the setting with the key key is null or does not exist.
Retrieving a nested setting
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::get('parent.child'); // or $value = setting('parent.child'); // or $value = setting()->get('parent.child');
This will return the value of the setting stored with the key parent.child.
Retrieving multiple settings via parent key
Imagine you have the following settings stored:
parent.child1with the valuevalue1parent.child2with the valuevalue2
use MangoldSecurity\Settings\Facades\Setting; $values = Setting::get('parent'); // or $values = setting('parent'); // or $values = setting()->get('parent');
This will return an associative array:
[
'child1' => 'value1',
'child2' => 'value2',
]
Retrieving multiple settings
use MangoldSecurity\Settings\Facades\Setting; $values = Setting::get(['parent.child1', 'parent.child2']); // or $values = setting(['parent.child1', 'parent.child2']); // or $values = setting()->get(['parent.child1', 'parent.child2']);
This will return an associative array:
[
'parent.child1' => 'value1',
'parent.child2' => 'value2',
]
Retrieving all settings
use MangoldSecurity\Settings\Facades\Setting; $values = Setting::get(); // or $values = setting()->get();
This will return all settings as an associative array.
Type safety
You can use the following methods to ensure type safety when retrieving settings:
String
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::string('key'); // or $value = setting()->string('key');
This will make sure the returned value is a string. Otherwise, it will throw an UnexpectedValueException.
Integer
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::integer('key'); // or $value = setting()->integer('key');
This will make sure the returned value is an integer. Otherwise, it will throw an UnexpectedValueException.
Float
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::float('key'); // or $value = setting()->float('key');
This will make sure the returned value is a float. Otherwise, it will throw an UnexpectedValueException.
Boolean
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::boolean('key'); // or $value = setting()->boolean('key');
This will make sure the returned value is a boolean. Otherwise, it will throw an UnexpectedValueException.
Array
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::array('key'); // or $value = setting()->array('key');
This will make sure the returned value is an array. Otherwise, it will throw an UnexpectedValueException.
Collection
use MangoldSecurity\Settings\Facades\Setting; $value = Setting::collection('key'); // or $value = setting()->collection('key');
This uses the same logic as array() but returns a Illuminate\Support\Collection instance instead of an array.
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
