initial commit
This commit is contained in:
32
app/Models/SystemSetting.php
Normal file
32
app/Models/SystemSetting.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user