44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use App\Models\SiteSetting;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class Settings
|
|
{
|
|
protected static $cacheKey = 'site_settings';
|
|
|
|
// Load settings from the database and cache them
|
|
public static function loadSettings()
|
|
{
|
|
$settings = SiteSetting::all()->pluck('setting_value', 'setting_key')->toArray();
|
|
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour
|
|
}
|
|
|
|
// Get a setting value by key
|
|
public static function get($key, $default = null)
|
|
{
|
|
$settings = Cache::get(self::$cacheKey, []);
|
|
return $settings[$key] ?? $default;
|
|
}
|
|
|
|
// Set a setting value by key
|
|
public static function set($key, $value)
|
|
{
|
|
// Update the database
|
|
SiteSetting::updateOrCreate(['setting_key' => $key], ['setting_value' => $value]);
|
|
|
|
// Update the cache
|
|
$settings = Cache::get(self::$cacheKey, []);
|
|
$settings[$key] = $value;
|
|
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour
|
|
}
|
|
|
|
// Clear the cache
|
|
public static function clearCache()
|
|
{
|
|
Cache::forget(self::$cacheKey);
|
|
}
|
|
}
|