123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace Dcat\Admin\Support;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Models\Setting as Model;
- use Illuminate\Database\QueryException;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Fluent;
- class Setting extends Fluent
- {
- /**
- * 获取配置.
- *
- * @param string $key
- * @param null $default
- *
- * @return mixed
- */
- public function get($key, $default = null)
- {
- return Arr::get($this->attributes, $key, $default);
- }
- /**
- * 设置配置信息.
- *
- * @param array $data
- *
- * @return $this
- */
- public function set($key, $value = null)
- {
- $data = is_array($key) ? $key : [$key => $value];
- foreach ($data as $key => $value) {
- Arr::set($this->attributes, $key, $value);
- }
- return $this;
- }
- /**
- * 保存配置到数据库
- *
- * @param array $data
- *
- * @return $this
- */
- public function save(array $data = [])
- {
- if ($data) {
- $this->set($data);
- }
- foreach ($this->attributes as $key => $value) {
- if (is_array($value)) {
- $value = json_encode($value);
- }
- $model = Model::query()
- ->where('slug', $key)
- ->first() ?: new Model();
- $model->fill([
- 'slug' => $key,
- 'value' => (string) $value,
- ])->save();
- }
- return $this;
- }
- /**
- * @return static
- */
- public static function fromDatabase()
- {
- $values = [];
- try {
- $values = Model::pluck('value', 'slug')->toArray();
- } catch (QueryException $e) {
- Admin::reportException($e);
- }
- return new static($values);
- }
- }
|