33 lines
786 B
PHP
33 lines
786 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class SystemSetting extends Model
|
|
{
|
|
protected $primaryKey = 'key';
|
|
|
|
public $incrementing = false;
|
|
|
|
protected $keyType = 'string';
|
|
|
|
protected $fillable = ['key', 'value'];
|
|
|
|
public static function get(string $key, mixed $default = null): mixed
|
|
{
|
|
return Cache::remember("setting.{$key}", 300, function () use ($key, $default) {
|
|
$row = static::query()->find($key);
|
|
|
|
return $row?->value ?? $default;
|
|
});
|
|
}
|
|
|
|
public static function set(string $key, mixed $value): void
|
|
{
|
|
static::query()->updateOrCreate(['key' => $key], ['value' => (string) $value]);
|
|
Cache::forget("setting.{$key}");
|
|
}
|
|
}
|