merge($this->syncValueJson($request->all())); return parent::store($request); } public function update(Request $request, $id) { $request->merge($this->syncValueJson($request->all())); return parent::update($request, $id); } public function showValue($key) { $query = Cnt::where('key', $key)->orderByDesc('id'); $columns = ['id', 'value', 'title_fa']; if (Cnt::hasValueJsonColumn()) { $columns[] = 'value_json'; } $cnt = $query->first($columns); if (! $cnt) { return response()->json(null); } $value = $cnt->value; if ( ($value === null || $value === '') && Cnt::hasValueJsonColumn() && is_array($cnt->value_json) ) { $value = json_encode($cnt->value_json, JSON_UNESCAPED_UNICODE); } return response()->json([ 'id' => $cnt->id, 'value' => $value, 'title_fa' => $cnt->title_fa, ]); } /** * ستون value_json را با JSON ذخیره‌شده در value همگام می‌کند. */ private function syncValueJson(array $data): array { if (! Cnt::hasValueJsonColumn() || ! array_key_exists('value', $data)) { return $data; } $raw = $data['value']; if (is_array($raw)) { $data['value_json'] = $raw; $data['value'] = json_encode($raw, JSON_UNESCAPED_UNICODE); return $data; } if (is_string($raw) && $raw !== '') { $decoded = json_decode($raw, true); if (is_array($decoded)) { $data['value_json'] = $decoded; } } return $data; } }