Files
jouwdriedee/app/Models/Setting.php
T
2026-10-31 14:23:53 +01:00

48 lines
1.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting 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
{
$setting = static::query()->find($key);
if (! $setting || $setting->value === null) {
return $default;
}
$decoded = json_decode($setting->value, true);
return (json_last_error() === JSON_ERROR_NONE) ? $decoded : $setting->value;
}
public static function set(string $key, mixed $value): static
{
$encoded = is_array($value) || is_object($value) ? json_encode($value) : (string) $value;
return static::query()->updateOrCreate(
['key' => $key],
['value' => $encoded]
);
}
public static function forget(string $key): bool
{
return (bool) static::query()->where('key', $key)->delete();
}
}