fix(const): return latest app_banners row and keep value_json in sync
Admin banner uploads were lost on refresh when an older const row or empty value column was served instead of the saved JSON. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -14,9 +14,72 @@ class ConstController extends SuperController
|
||||
parent::__construct(Cnt::class, [], ['*'], null, null, ['key']);
|
||||
}
|
||||
|
||||
public function showValue($key){
|
||||
$cnt = Cnt::where('key',$key)
|
||||
->select('id','value','title_fa')->first();
|
||||
return response()->json($cnt);
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user