getMany($key); } if (! array_key_exists($key, $this->items)) { try { $this->items[$key] = $this->cache->remember($this->cacheKey($key), $this->ttl, function () use ($key) { $settings = Setting::where('key', $key)->firstOrFail(); return $settings->value; }); } catch (ModelNotFoundException $e) { return $default; } } return $this->items[$key]; } /** * @param array $keys * @return array */ public function getMany(array $keys) { $settings = []; foreach ($keys as $key => $default) { if (is_numeric($key)) { [$key, $default] = [$default, null]; } $settings[$key] = $this->get($key, $default); } return $settings; } /** * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null) { $keys = is_array($key) ? $key : [$key => $value]; foreach ($keys as $key => $value) { Setting::updateOrCreate([ 'key' => $key, ], [ 'value' => $value, ]); $this->cleanCache($key); } } /** * @param string $key * @return void */ protected function cleanCache(string $key): void { unset($this->items[$key]); $this->cache->forget($this->cacheKey($key)); } /** * @param string $key * @return string */ protected function cacheKey(string $key): string { return "settings.{$key}"; } }