mailwolt/app/Models/Setting.php

34 lines
962 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
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]);
}
public static function signupAllowed()
{
$value = self::where('key', 'signup_enabled')->value('value');
return is_null($value) || (int) $value === 1;
}
}