コード例 #1
0
 /**
  * Find a setting by it's key
  * or return a new instance.
  *
  * @param $key
  * @return SettingInterface
  */
 public function findByKeyOrNew($key)
 {
     if (!($setting = $this->model->where('key', $key)->first())) {
         $setting = $this->model->newInstance();
         $setting->setKey($key);
     }
     return $setting;
 }
コード例 #2
0
 /**
  * Set a setting value.
  *
  * @param $key
  * @param $value
  * @return $this
  */
 public function set($key, $value)
 {
     $setting = $this->model->where('key', $key)->first();
     if (!$setting) {
         $setting = $this->model->newInstance();
         $setting->key = $key;
     }
     if (!($field = config(str_replace('::', '::settings/settings.', $key)))) {
         $field = config(str_replace('::', '::settings.', $key));
     }
     if (is_string($field)) {
         $field = ['type' => $field];
     }
     $type = app(array_get($field, 'type'));
     $modifier = $type->getModifier();
     if ($modifier instanceof FieldTypeModifier) {
         $value = $modifier->modify($value);
     }
     $setting->value = $value;
     $setting->save();
     return $this;
 }