English Document | 繁體中文文件
A Laravel package designed for managing system configuration values in database.
Run the Composer require command from the Terminal:
composer require hankz/laravel-system-settings
This package supports Laravel's auto-discovery feature and it's ready to use once installed.
You need publish the config file.
php artisan vendor:publish --provider="Hankz\LaravelSystemSettings\SystemSettingProvider"
You can configure this package in config\system-settings.php
<?php
return [
/**
* use table name.
*/
'table_name' => 'system_settings',
/**
* default value.
*/
'default' => [
'group' => 'default',
],
];
- table_name - Save the system settings table name in the database.
- default.group - The default group name when you are missing the group parameter.
You should also run the migrate command.
php artisan migrate
This package is very simple and provides the following methods for your use.
Create a new system setting.
$key
(string): The unique identifier for the system setting.$value
(string): The value associated with the system setting.$group
(string|null): (Optional) The group to which the system setting belongs. Default isnull
.$description
(string|null): (Optional) A description providing additional information about the system setting. Default isnull
.
SystemSettingManager::new('keyName', 'value', 'groupName', 'description');
bool
: Returnstrue
if the system setting is created successfully, or throws aSystemSettingAlreadyExistsException
if the setting already exists.
The has()
method checks if a system setting exists with the given key and optional group.
$key
(string): The unique identifier for the system setting.$group
(string|null): (Optional) The group to which the system setting belongs. Default isnull
.
SystemSettingManager::has('keyName', 'groupName');
bool
: Returnstrue
if the system setting exists, otherwisefalse
.
Return all system settings with key and value in the format of key-value pairs.
SystemSettingManager::all();
array
: An associative array containing all system settings in the format['key' => 'value']
.
The get()
method allows you to retrieve the value using both the key name and group name, and it also accepts a default value.
$key
(string): The unique identifier for the system setting.$group
(string|null): (Optional) The group to which the system setting belongs. Default isnull
.$default
(string|null): (Optional) The default value to return if the system setting is not found. Default isnull
.
SystemSettingManager::get('keyName', 'groupName', 'defaultValue');
string|null
: Returns the value of the system setting if found, otherwise returns the specified default value ornull
if no default is provided.
set(string $key, string $value, string $group = null, string $description = null, bool $createWhenNotExist = false): void
The set()
method allows you to update the value and description of an existing system setting or create a new one if it does not exist.
$key
(string): The unique identifier for the system setting.$value
(string): The new value to set for the system setting.$group
(string|null): (Optional) The group to which the system setting belongs. Default isnull
.$description
(string|null): (Optional) A description providing additional information about the system setting. Default isnull
.$createWhenNotExist
(bool): (Optional) If set totrue
, a new system setting will be created if the specified key does not exist. Default isfalse
.
SystemSettingManager::set('keyName', 'newValue', 'groupName', 'newDescription', true);
void
Retrieve all system settings within a specified group.
$group
(string): The group from which to retrieve system settings.
SystemSettingManager::getByGroup('groupName');
array
: An associative array containing system settings within the specified group in the format['key' => 'value']
.
Update or create multiple system settings within a specified group.
$settings
(array): An associative array containing system settings in the format['key' => ['value' => '', 'description' => '']]
.$group
(string|null): (Optional) The group to which the system settings belong. Default isnull
.$createWhenNotExist
(bool): (Optional) If set totrue
, new system settings will be created for keys that do not exist in the specified group. Default isfalse
.
SystemSettingManager::setByGroup(['key1' => ['value' => 'value1', 'description' => 'desc1']], 'groupName', true);
void
Delete a system setting with the specified key and optional group.
$key
(string): The unique identifier for the system setting.$group
(string|null): (Optional) The group to which the system setting belongs. Default isnull
.
SystemSettingManager::delete('keyName', 'groupName');
bool|null
: Returnstrue
if the system setting is successfully deleted,false
if the setting does not exist, ornull
if an error occurs.
- Add audit log
MIT License © 2023 Hankz