【发布时间】:2019-08-30 01:33:28
【问题描述】:
尝试将两个 JSON 响应合并为一个,但问题是数据显示在两个数组中,我希望它在一个数组中。我如何在 Lumen/Laravel 中实现它
尝试污染两个数组或响应
public function index(Request $request)
{
$post = Post::orderBy('id', 'DESC')->get();
$post_images = PostImage::orderBy('id', 'DESC')->get();
return $this->successResponse($posts.$post_image );
}
预期:-
{
"post": {
"id": 14,
"post_id": 798965728,
"user_id": 1,
"location": "first",
"title": "un8852",
"cooked_time": "1554329910",
"dispose_time": "1554373110",
"food_type": "nv",
"description": "asdfg",
"serve_quantity": 23,
"lat": 19.08,
"lon": 73,
"id": 10,
"post_id": 798965728,
"image_name1": null,
"image_name2": null,
"image_name3": null,
"image_name4": null,
"image_name5": null,
},
"st": "1",
"msg": "success"
}
得到:-
{
"post":"{\"id\":14,\"post_id\":798965728,\"user_id\":1,\"location\":\"first\",\"title\":\"un8852\",\"cooked_time\":\"1554329910\",\"dispose_time\":\"1554373110\",\"food_type\":\"nv\",\"description\":\"asdfg\",\"serve_quantity\":23,\"lat\":19.08,\"lon\":73,\"created_at\":\"2019-04-04 10:20:18\",\"updated_at\":\"2019-04-04 10:20:18\"}{\"id\":10,\"post_id\":798965728,\"image_name1\":null,\"image_name2\":null,\"image_name3\":null,\"image_name4\":null,\"image_name5\":null,\"created_at\":\"2019-04-04 10:20:18\",\"updated_at\":\"2019-04-04 10:20:18\"}",
"st":"1",
"msg":"success"
}
【问题讨论】:
-
如果您包含源数据可能会有所帮助,我们有预期结果,实际结果,但不是[源数据] - 这很好......有点重要。
-
我可以告诉你,你会很想在
json_decoding之后使用array_replace_recursive($primary, $secondary)。 PS prolly 不是一个词,但我喜欢它。好的..原来是oxforddictionaries :-( -
@ArtisticPhoenix 你能帮我解决这种问题吗stackoverflow.com/questions/55593703/…
-
不幸的是我还没有使用 Laravel,我实际上正处于使用它完成项目的计划阶段,但我们将它最初外包,因为我太忙于其他事物。但我计划稍后在它的基础上进行构建,基本上它是我们传入数据的 CRUD 系统,这应该很容易做到。然后后来我种植在它上面。目前我们主要使用 Code Igniter 2,它是我大约 5 年前构建并继续改进的。
标签: php laravel laravel-5.2 lumen lumen-5.4