getTableColumns(); $model = $this->getModel(); $isEnable = Arr::get($data, 'is_enabled'); $publishedAt = Arr::get($data, 'published_at'); if ($isEnable && empty($publishedAt)) { $data['published_at'] = now(); } if (isset($data['resource'])) { $data['resource'] = $this->saveImage('resource', 'ads/resource')[0]; } //处理跳转配置 $jumpType = Arr::get($data, 'jump_type'); if ($jumpType !== null) { switch ($jumpType) { case Ad::TYPE_OFF: $data['jump_config'] = null; break; case Ad::TYPE_WEB: $data['jump_config'] = Arr::only($data['jump_config'], 'web_link'); break; case Ad::TYPE_APP: $data['jump_config'] = Arr::only($data['jump_config'], 'app_link'); break; case Ad::TYPE_MINI: $data['jump_config'] = Arr::only($data['jump_config'], ['mini_id', 'mini_link']); break; } } foreach ($data as $k => $v) { if (! in_array($k, $columns)) { continue; } $model->setAttribute($k, $v); } return $model->save(); } public function update($primaryKey, $data): bool { $columns = $this->getTableColumns(); $model = $this->query()->whereKey($primaryKey)->first(); $isEnable = Arr::get($data, 'is_enable'); $publishedAt = Arr::get($data, 'published_at'); if ($isEnable && empty($publishedAt) && empty($model->published_at)) { $data['published_at'] = now(); } if (isset($data['resource'])) { $data['resource'] = $this->saveImage('resource', 'ads/resource')[0]; } //处理跳转配置 $jumpType = Arr::get($data, 'jump_type'); if ($jumpType !== null) { switch ($jumpType) { case Ad::TYPE_OFF: $data['jump_config'] = null; break; case Ad::TYPE_WEB: $data['jump_config'] = Arr::only($data['jump_config'], 'web_link'); break; case Ad::TYPE_APP: $data['jump_config'] = Arr::only($data['jump_config'], 'app_link'); break; case Ad::TYPE_MINI: $data['jump_config'] = Arr::only($data['jump_config'], ['mini_id', 'mini_link']); break; } } foreach ($data as $k => $v) { if (! in_array($k, $columns)) { continue; } $model->setAttribute($k, $v); } return $model->save(); } }