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