Files
toolbox/app/Services/ConfigService.php
2025-12-18 10:18:25 +08:00

44 lines
1.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Config;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Schema;
class ConfigService
{
public function get(string $key, mixed $default = null): mixed
{
$config = Config::query()->where('key', $key)->first();
return $config?->value ?? $default;
}
public function getNested(string $key, string $path, mixed $default = null): mixed
{
$value = $this->get($key);
return Arr::get($value ?? [], $path, $default);
}
public function set(string $key, mixed $value, ?string $description = null): Config
{
return Config::query()->updateOrCreate(
['key' => $key],
[
'value' => $value,
'description' => $description,
]
);
}
public function setNested(string $key, string $path, mixed $value, ?string $description = null): Config
{
$payload = $this->get($key, []);
Arr::set($payload, $path, $value);
return $this->set($key, $payload, $description);
}
}