Files
star-erp/app/Modules/Core/Models/SystemSetting.php
sky121113 e3df090afd
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
feat: 統一各模組分頁組件佈局並新增系統設定功能相關檔案
2026-02-25 16:16:49 +08:00

62 lines
1.4 KiB
PHP

<?php
namespace App\Modules\Core\Models;
use Illuminate\Database\Eloquent\Model;
class SystemSetting extends Model
{
protected $fillable = [
'group',
'key',
'value',
'type',
'description',
];
/**
* 同請求內的記憶體快取,避免重複查詢 DB
* PHP 請求結束後自動釋放,無需額外處理失效
*/
protected static array $cache = [];
/**
* 取得特定設定值(含記憶體快取)
*/
public static function getVal(string $key, $default = null)
{
if (array_key_exists($key, static::$cache)) {
return static::$cache[$key];
}
$setting = self::where('key', $key)->first();
if (!$setting) {
static::$cache[$key] = $default;
return $default;
}
$value = $setting->value;
// 根據 type 進行類別轉換
$resolved = match ($setting->type) {
'integer', 'number' => (int) $value,
'boolean', 'bool' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
'json', 'array' => json_decode($value, true),
default => $value,
};
static::$cache[$key] = $resolved;
return $resolved;
}
/**
* 清除記憶體快取(儲存設定後應呼叫)
*/
public static function clearCache(): void
{
static::$cache = [];
}
}