只有 JSON 中的第一个值保存到数据库 - Laravel
Posted
技术标签:
【中文标题】只有 JSON 中的第一个值保存到数据库 - Laravel【英文标题】:Only the first value in the JSON saved to Database - Laravel 【发布时间】:2021-01-08 14:15:42 【问题描述】:我正在从我试图将数据保存到数据库的表单中接收数据。
在保存第一条记录时会保存多次。
return response()->json($request->all());
JSON
"product_combo_name":"asdfadsf",
"product_values":"[\"product_data\":\"id\":1,\"slug\":\"product-name\",\"sku\":\"product-code\",\"product_name\":\"Product Name\",\"category_id\":1,\"subcategory_id\":1,\"previewImg\":\"http:\/\/localhost\/EcommerceBackend\/public\/\/assets\/images\/product\/thumb\/Product Name.jpeg\",\"hasVariants\":false,\"deleted_at\":null,\"created_at\":\"2020-09-17T07:17:42.000000Z\",\"updated_at\":\"2020-09-17T10:54:24.000000Z\",\"product_qty\":\"1\",\"product_price\":\"100\",\"product_tax\":\"18\",\"hsn_code\":\"78877\",\"product_line_total\":\"118.00\",\"product_data\":\"id\":1,\"slug\":\"product-name\",\"sku\":\"product-code\",\"product_name\":\"Product Name\",\"category_id\":1,\"subcategory_id\":1,\"previewImg\":\"http:\/\/localhost\/EcommerceBackend\/public\/\/assets\/images\/product\/thumb\/Product Name.jpeg\",\"hasVariants\":false,\"deleted_at\":null,\"created_at\":\"2020-09-17T07:17:42.000000Z\",\"updated_at\":\"2020-09-17T10:54:24.000000Z\",\"product_qty\":\"1\",\"product_price\":\"100\",\"product_tax\":\"18\",\"hsn_code\":\"197797\",\"product_line_total\":\"118.00\"]",
"shipping_charges":"123",
"total_price":"359.00",
"combo_total_price":"124.18",
"product_total_price":"236.00",
"charge_breakup":"[\"name\":\"qty\",\"qty\":\"1\",\"per_unit_price\":\"1\",\"tax\":\"18\",\"tax_amount\":\"0.18\",\"line_total_amount\":\"1.18\"]"
我正在尝试将 product_data 保存为单个值,但 JSON 对我来说似乎有点复杂。
当我在 Laravel Controller 中运行 foreach 时,只返回第一个对象,
foreach (json_decode($request->product_values) as $key => $value)
# code...
Product::insert([
'product_id' => $value->id,
]);
return response()->json(json_encode($value->product_data->id));
Response : "1"
charge_breakup 的行为相同。不知道如何破解这个 JSON 响应。
编辑 1:
$value = json_decode($request->product_values);
return response()->json($value);
Json 响应:
["product_data":"id":1,"slug":"product-name","sku":"product-code","product_name":"Product Name","category_id":1,"subcategory_id":1,"previewImg":"http:\/\/localhost\/EcommerceBackend\/public\/\/assets\/images\/product\/thumb\/Product Name.jpeg","hasVariants":false,"deleted_at":null,"created_at":"2020-09-17T07:17:42.000000Z","updated_at":"2020-09-17T10:54:24.000000Z","product_qty":"1","product_price":"100","product_tax":"18","hsn_code":"78877","product_line_total":"118.00","product_data":"id":1,"slug":"product-name","sku":"product-code","product_name":"Product Name","category_id":1,"subcategory_id":1,"previewImg":"http:\/\/localhost\/EcommerceBackend\/public\/\/assets\/images\/product\/thumb\/Product Name.jpeg","hasVariants":false,"deleted_at":null,"created_at":"2020-09-17T07:17:42.000000Z","updated_at":"2020-09-17T10:54:24.000000Z","product_qty":"1","product_price":"100","product_tax":"18","hsn_code":"197797","product_line_total":"118.00"]
【问题讨论】:
您只获得第一个 ID,因为您在 foreach 中返回。您是否尝试过移动 return 语句? 不要在循环内返回,它只会在第一次运行时打破循环 用 foreach 之外的结果更新了问题 【参考方案1】:你只似乎得到一件物品的原因是因为你在 foreach 内返回。
foreach (json_decode($request->product_values) as $key => $value)
# code...
Product::insert([
'product_id' => $value->id,
]);
return response()->json(json_encode($value->product_data->id));
我不确定你想在这里实现什么,但要获取 id 的集合:
$ids = array();
foreach (json_decode($request->product_values) as $key => $value)
# code...
Product::insert([
'product_id' => $value->id,
]);
array_push($ids, json_encode($value->product_data->id));
return response()->json(collect($ids));
【讨论】:
以上是关于只有 JSON 中的第一个值保存到数据库 - Laravel的主要内容,如果未能解决你的问题,请参考以下文章
在 Xamarin.Android 中将 Json 对象项中的错误值保存和检索到 IShared 首选项(始终返回注销值)