我有一个数据库表,它的类型是longtext,因为我将结构更改为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或仅使用本地php更新来自mysql数据库的JSON数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58197467/