I need to update my data in json column.
Code
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.'
]);
}
}
I have all my data ready in controller and there is no issue with data, all I need is to update my json data mentioned in this part:
$item = CartStorage::findOrFail($request->input('id'));
$item->update([
// I need this to happen
// cart_data->quantity= $qty;
]);
Database screenshot
Database data cart_data column
"{
"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
}
]
}"
Any idea?
Advertisement
Answer
The answer from @Kurt Friars is good. If you needed to pull the CartStorage model for some reason and do other things with it, you could update the cart_data like this:
$item = CartStorage::findOrFail($request->input('id'));
// Do whatever else you need
$cartData = $item->cart_data;
$cartData['quantity'] = 4;
$item->update([
'cart_data' => $cartData
]);
