mailwolt/app/Models/Setting.php

26 lines
745 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = ['key','value'];
public $incrementing = false;
protected $keyType = 'string';
public static function get(string $key, $default=null) {
$row = static::query()->find($key);
if (!$row) return $default;
$val = $row->value;
$decoded = json_decode($val, true);
return (json_last_error() === JSON_ERROR_NONE) ? $decoded : $val;
}
public static function set(string $key, $value): void {
$val = is_array($value) ? json_encode($value, JSON_UNESCAPED_SLASHES) : (string)$value;
static::query()->updateOrCreate(['key'=>$key], ['value'=>$val]);
}
}