34 lines
739 B
PHP
34 lines
739 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Setting extends Model
|
|
{
|
|
protected $fillable = ['key', 'value', 'group'];
|
|
|
|
/**
|
|
* 读取设置值,未设置时返回默认值。
|
|
*/
|
|
public static function get(string $key, ?string $default = null): ?string
|
|
{
|
|
$row = static::where('key', $key)->first();
|
|
if (!$row) {
|
|
return $default;
|
|
}
|
|
return $row->value;
|
|
}
|
|
|
|
/**
|
|
* 写入或更新设置值。
|
|
*/
|
|
public static function set(string $key, ?string $value, string $group = 'general'): void
|
|
{
|
|
static::updateOrCreate(
|
|
['key' => $key],
|
|
['value' => $value, 'group' => $group]
|
|
);
|
|
}
|
|
}
|