初始化
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?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]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user