45 lines
1.0 KiB
PHP
45 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class AppSetting extends Model
|
|
{
|
|
protected $fillable = [
|
|
'key',
|
|
'value',
|
|
'type',
|
|
'group',
|
|
'label',
|
|
'description',
|
|
];
|
|
|
|
public static function get(string $key, $default = null)
|
|
{
|
|
$setting = Cache::remember("setting.$key", 3600, fn () => static::where('key', $key)->first());
|
|
|
|
if (! $setting) {
|
|
return $default;
|
|
}
|
|
|
|
return match ($setting->type) {
|
|
'integer' => (int) $setting->value,
|
|
'boolean' => filter_var($setting->value, FILTER_VALIDATE_BOOLEAN),
|
|
default => $setting->value,
|
|
};
|
|
}
|
|
|
|
public static function set(string $key, $value): void
|
|
{
|
|
static::where('key', $key)->update(['value' => $value]);
|
|
Cache::forget("setting.$key");
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saved(fn ($m) => Cache::forget("setting.{$m->key}"));
|
|
}
|
|
}
|