2015-12-13 12:42:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-11-14 17:57:25 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2015-12-13 12:42:28 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2017-12-09 19:34:27 +01:00
|
|
|
/**
|
|
|
|
* @property string $key
|
2021-06-05 12:47:56 +02:00
|
|
|
* @property mixed $value
|
2019-08-05 17:57:36 +07:00
|
|
|
*
|
2019-08-05 17:56:48 +07:00
|
|
|
* @method static self find(string $key)
|
2017-12-09 19:34:27 +01:00
|
|
|
*/
|
2015-12-13 12:42:28 +08:00
|
|
|
class Setting extends Model
|
|
|
|
{
|
2020-11-14 17:57:25 +01:00
|
|
|
use HasFactory;
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2020-11-14 17:57:25 +01:00
|
|
|
protected $primaryKey = 'key';
|
2022-07-07 23:23:41 +02:00
|
|
|
protected $keyType = 'string';
|
2015-12-13 12:42:28 +08:00
|
|
|
public $timestamps = false;
|
|
|
|
protected $guarded = [];
|
|
|
|
|
2022-07-07 23:23:41 +02:00
|
|
|
protected $casts = ['value' => 'json'];
|
|
|
|
|
2022-07-07 12:45:47 +02:00
|
|
|
public static function get(string $key): mixed
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2022-07-07 12:45:47 +02:00
|
|
|
return self::find($key)?->value;
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a setting (no pun) value.
|
|
|
|
*
|
2022-07-07 12:45:47 +02:00
|
|
|
* @param array|string $key the key of the setting, or an associative array of settings,
|
2020-09-06 23:20:42 +02:00
|
|
|
* in which case $value will be discarded
|
2015-12-13 12:42:28 +08:00
|
|
|
*/
|
2024-02-24 01:36:02 +07:00
|
|
|
public static function set(array|string $key, $value = ''): void
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
|
|
|
if (is_array($key)) {
|
|
|
|
foreach ($key as $k => $v) {
|
|
|
|
self::set($k, $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-09 20:45:11 +02:00
|
|
|
self::query()->updateOrCreate(compact('key'), compact('value'));
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|
|
|
|
}
|