You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
56 lines
1.2 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class SystemSetting extends Model
|
|
{
|
|
protected $primaryKey = 'key';
|
|
|
|
public $incrementing = false;
|
|
|
|
protected $keyType = 'string';
|
|
|
|
protected $fillable = [
|
|
'key',
|
|
'value',
|
|
];
|
|
|
|
public static function getInt(string $key, int $default = 0): int
|
|
{
|
|
$row = static::query()->find($key);
|
|
if ($row === null || $row->value === null || $row->value === '') {
|
|
return $default;
|
|
}
|
|
|
|
return (int) $row->value;
|
|
}
|
|
|
|
public static function getString(string $key, ?string $default = null): ?string
|
|
{
|
|
$row = static::query()->find($key);
|
|
if ($row === null || $row->value === null || $row->value === '') {
|
|
return $default;
|
|
}
|
|
|
|
return (string) $row->value;
|
|
}
|
|
|
|
public static function setInt(string $key, int $value): void
|
|
{
|
|
static::query()->updateOrCreate(
|
|
['key' => $key],
|
|
['value' => (string) $value],
|
|
);
|
|
}
|
|
|
|
public static function setString(string $key, ?string $value): void
|
|
{
|
|
static::query()->updateOrCreate(
|
|
['key' => $key],
|
|
['value' => $value ?? ''],
|
|
);
|
|
}
|
|
}
|