webmoney/backend/app/Models/SiteSetting.php

53 lines
1.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SiteSetting extends Model
{
protected $fillable = [
'key',
'value',
'description',
];
protected $casts = [
'value' => 'array',
];
/**
* Get a setting value by key
*/
public static function getValue(string $key, $default = null)
{
$setting = static::where('key', $key)->first();
if (!$setting) {
return $default;
}
// If value is a simple string stored as JSON array, extract it
$value = $setting->value;
if (is_array($value) && isset($value['value'])) {
return $value['value'];
}
return $value ?? $default;
}
/**
* Set a setting value by key
*/
public static function setValue(string $key, $value, string $description = null): self
{
return static::updateOrCreate(
['key' => $key],
[
'value' => is_array($value) ? $value : ['value' => $value],
'description' => $description,
]
);
}
}