第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Laravel 中更新深層 json 數(shù)據(jù)

如何在 Laravel 中更新深層 json 數(shù)據(jù)

PHP
手掌心 2023-07-15 18:37:23
我需要更新 json 列中的數(shù)據(jù)。代碼public function update(Request $request){    $product = Product::findOrFail($request->input('productId'));    $stock = $product->qty;    $qty = $request->input('quantity');    $userId = auth('api')->user()->id;        if (!empty($qty)) {        if ($qty < $stock) {            $item = CartStorage::findOrFail($request->input('id'));            $item->update([                // I need this to happen                // cart_data->quantity= $qty;            ]);            return response()->json([                'data' => $item,                'success' => 'Updated'            ]);        } else {            return response()->json([                'success' => 'Your quantity request is larger than our stock.'            ]);        }    } else{ // 1. if nothing is given        return response()->json([            'success' => 'You need to input new quantity.'        ]);    }}我已在控制器中準備好所有數(shù)據(jù),并且數(shù)據(jù)沒有問題,我所需要做的就是更新本部分中提到的 json 數(shù)據(jù):$item = CartStorage::findOrFail($request->input('id'));$item->update([  // I need this to happen  // cart_data->quantity= $qty;]);數(shù)據(jù)庫截圖數(shù)據(jù)庫數(shù)據(jù)cart_data列"{    \"name\":\"Option Product\",    \"productId\":21,    \"price\":\"370000\",    \"quantity\":2,           ------> trying to update this value    \"attributes\":{        \"attr\":{            \"name\":\"weight\",            \"value\":\"2\"        }    },    \"conditions\":[        {            \"name\":\"Colors\",            \"value\":0        }    ]}"任何想法?
查看完整描述

2 回答

?
德瑪西亞99

TA貢獻1770條經(jīng)驗 獲得超3個贊

如果您出于某種原因需要拉取 CartStorage 模型并用它執(zhí)行其他操作,您可以像這樣更新 cart_data:


$item = CartStorage::findOrFail($request->input('id'));


// Do whatever else you need


$cartData = $item->cart_data;

$cartData['quantity'] = 4;


$item->update([

    'cart_data' => $cartData

]);


查看完整回答
反對 回復(fù) 2023-07-15
?
慕萊塢森

TA貢獻1810條經(jīng)驗 獲得超4個贊

您可以直接在查詢生成器中更新 JSON ,而無需先檢索它,如下所示:

// Assuming pk on cart_storages is id

DB::table('cart_storages')

? ?->where('id', $request->input('id'))

? ?->update([

? ? ? ?'cart_data->quantity' => $qty,

? ?]);

重要的是要聲明文檔聲明“MySQL 5.7+ 和 PostgreSQL 9.5+ 支持此操作”


查看完整回答
反對 回復(fù) 2023-07-15
  • 2 回答
  • 0 關(guān)注
  • 210 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號