[英]Inserting JSON object in MySQL in Laravel
I am working on inserting a JSON object
in JSON-type MySQL column
. 我正在努力在
JSON-type MySQL column
插入JSON object
。
Sample data that I am trying to insert is like this: 我尝试插入的示例数据如下所示:
Sample Laravel code 示例Laravel代码
public function store(Request $request)
{
$layaway = new Layaway;
foreach($request->selected_products as $p) {
$layaway->selected_products = json_encode([
'id' => $p['id'],
'sku' => $p['sku'],
'name' => $p['name'],
'image' => $p['image'],
'description' => $p['description'],
'category_id' => $p['category_id'],
'regular_price' => $p['regular_price']
]);
}
$layaway->save();
return response()->json([
'success' => true,
'message' => 'Layaway Program has been successfully saved!'
]);
}
Sample response from dd() 来自dd()的样本响应
Question How can I save all of the data in the $request->selected_products
into my MYSQL json-type column
? 问题如何将
$request->selected_products
中的所有数据保存到MYSQL json-type column
?
As said in the comment by Wreigh, 如Wreigh的评论所述,
I just changed this code: 我只是更改了这段代码:
foreach($request->selected_products as $p) {
$layaway->selected_products = json_encode([
'id' => $p['id'],
'sku' => $p['sku'],
'name' => $p['name'],
'image' => $p['image'],
'description' => $p['description'],
'category_id' => $p['category_id'],
'regular_price' => $p['regular_price']
]);
}
into this code: 变成这段代码:
$layaway->selected_products = json_encode($request->selected_products);
Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.