In Lara Dashboard we’ve Settings API, so that you can change/add any settings easily. You can imagine settings as key-value pair in database.
Backend architecture #
We’ve Settings model and settings table and a SettingsService class, which are responsible to handle the settings in Lara Dashboard.
The Settings feature in this application is designed to manage and store various configuration options. It consists of a database table and a corresponding Eloquent model to interact with the data.
Database Table: settings
#
The settings
table is responsible for storing all the configuration options. Below is the structure of the table:
Column Name | Data Type | Description |
---|---|---|
id | bigint | Primary key of the table. |
option_name | string | The unique name of the setting (e.g., site_name , site_logo ). |
option_value | text | The value of the setting (e.g., My Website , URL of a logo). |
autoload | boolean | Indicates whether the setting should be automatically loaded (default: false ). |
created_at | timestamp | Timestamp when the setting was created. |
updated_at | timestamp | Timestamp when the setting was last updated. |
Model: Setting
#
The Setting
model provides an interface to interact with the settings
table. It allows you to perform CRUD operations on the settings data.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = [
'option_name',
'option_value',
'autoload',
];
}
- Fillable Attributes:
Thefillable
property ensures that only the specified attributes (option_name
,option_value
,autoload
) can be mass-assigned.
Settings API #
There are general settings apis, which are responsible to do settings manipulation from any part of the application very easily. You can do this very easily from your custom module.
Add Settings #
Adds a new setting or updates an existing one – add_settings()
add_setting(string $optionName, mixed $optionValue, bool $autoload = false): void
// Example: add_setting('site_name', 'My Cool Site');
Full Example how to use Add settings #
$settingsRequest = [
'settings_key1' => 'Key 1 value',
'settings_key2' => 'Key 2 value',
]; // key value pair.
foreach($settingsRequest as $key => $value) {
add_setting($key, $value);
}
Update Settings #
Updates an existing setting. If the setting doesn’t exist, returns false
. The autoload value will remain unchanged if null
is passed.
update_setting(string $optionName, mixed $optionValue, ?bool $autoload = null): bool
// Example: update_setting('app_theme', 'light');
Delete Settings #
Deletes a setting from the settings table if it exists.
delete_setting(string $optionName): bool
// Example: delete_setting('maintenance_mode');
Get Setting #
Fetches the value of a single setting by its name.
get_setting('app_theme')
Get Settings #
Returns a list of settings filtered by autoload status.
$autoloaded = get_settings(); // Only autoloaded
$nonAutoloaded = get_settings(false); // Only non-autoloaded
$all = get_settings(-1); // All settings
Under the Hood #
Lets know the under the hoods architecture of settings api. We’ll share the code snippet of core, but it could be different in core as the below code maybe not updated all the time, but the overall concept would be same as always.
Settings Service #
To get more knowledge how its handled, you can get closer look from Settings Service class –
<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\Setting;
class SettingService
{
public array $excluded_settings = [];
public function __construct()
{
$this->excluded_settings = ld_apply_filters('excluded_setting_keys', [
'_token',
]);
}
public function addSetting(string $optionName, mixed $optionValue, bool $autoload = false): ?Setting
{
if (in_array($optionName, $this->excluded_settings)) {
return null;
}
return Setting::updateOrCreate(
['option_name' => $optionName],
['option_value' => $optionValue ?? '', 'autoload' => $autoload]
);
}
public function updateSetting(string $optionName, mixed $optionValue, ?bool $autoload = null): bool
{
if (in_array($optionName, $this->excluded_settings)) {
return false;
}
$setting = Setting::where('option_name', $optionName)->first();
if ($setting) {
$setting->update([
'option_value' => $optionValue,
'autoload' => $autoload ?? $setting->autoload,
]);
return true;
}
return false;
}
public function deleteSetting(string $optionName): bool
{
return Setting::where('option_name', $optionName)->delete() > 0;
}
public function getSetting(string $optionName): mixed
{
return Setting::where('option_name', $optionName)->value('option_value');
}
public function getSettings(int|bool|null $autoload = true): array
{
if ($autoload === -1) {
return Setting::all()->toArray();
}
return Setting::where('autoload', (bool) $autoload)->get()->toArray();
}
}
Simple function #
Then to make it accessible to everywhere without model calling –
function add_setting(string $optionName, mixed $optionValue, bool $autoload = false): void
{
app(App\Services\SettingService::class)->addSetting($optionName, $optionValue, $autoload);
}
So, this is a general conclusion about Settings in Lara dashboard. It could help you to do things quickly in your custom module or anywhere.
Happy Coding…