resolved !== null) { return $this->resolved; } $defaults = $this->defaults(); if (!$this->tableExists()) { return $this->resolved = $defaults; } $stored = StoreSetting::query() ->pluck('value', 'key') ->map(fn ($value) => (string) $value) ->all(); return $this->resolved = array_replace($defaults, $stored); } public function get(string $key, mixed $default = null): mixed { $settings = $this->all(); return $settings[$key] ?? $default; } public function defaults(): array { return (array) config('store.settings_defaults', []); } public function keys(): array { return array_keys($this->defaults()); } public function setMany(array $values): void { if (!$this->tableExists()) { throw new RuntimeException('store_settings table is missing. Please run php artisan migrate.'); } $now = now(); $rows = collect($values) ->only($this->keys()) ->map(fn ($value, $key) => [ 'key' => $key, 'value' => (string) $value, 'created_at' => $now, 'updated_at' => $now, ]) ->values() ->all(); if ($rows === []) { return; } StoreSetting::query()->upsert($rows, ['key'], ['value', 'updated_at']); $this->resolved = null; } private function tableExists(): bool { try { return Schema::hasTable('store_settings'); } catch (Throwable) { return false; } } }