gpt4 book ai didi

php - 如何使用 laravel 或仅使用 native php 更新来自 mysql 数据库的 JSON 数据?

转载 作者:行者123 更新时间:2023-11-29 02:09:13 25 4
gpt4 key购买 nike

自从我将结构更改为 JSON 以来,我有一个长文本类型的数据库表,因为我将把 JSON 数据放在那里,然后插入就可以了。我的问题是我想在使用 laravel/php 检索该 JSON 数据后更新它的一部分。

我尝试使用下面的代码,但我很难完成它。

$qtyy = $orderDetail->quantity;  
$product = Product::findOrFail($prod_id);
$json = json_decode($product->variations, true);
foreach ($json as $key => $value) {
if($key == $orderDetail->variation){
$total = (int)$value->qty + (int)$qtyy;
DB::table('products')
->where('id', $prod_id)
->update([$value->qty => $total]);
}
}

我想更改数据库中的“数量”

{ 
"AliceBlue-Cream":{
"price":"500",
"sku":"V-AliceBlue-Cream",
"qty":"1000"
},
"Amethyst-Cream":{
"price":"500",
"sku":"V-Amethyst-Cream",
"qty":"2998" <- I want to update that
}
}

最佳答案

试试这个,对我有用!

DB::table('products')  
->where('id', $prod_id)
->update([
"Amethyst-Cream->qty" => "2998"
]);

关于php - 如何使用 laravel 或仅使用 native php 更新来自 mysql 数据库的 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58197467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com