$this->getCachedCategories();//Above code Stores the data in cache for future use. So, it goes to the database only //one time and next time pick the data from cache. So far everything is good.//I have a search criteria which is in the form of array. I filter above cache data based //upon the search criteria and gets the data.foreach ($userInputsForFilter as $key => $value) { $this->Categories = $this->Categories->where($key, $value);}这是屏幕截图。如果您注意到检索到的数据的第一个索引是 1 而不是 0。实际上第二个记录是在过滤缓存数据后出现的。你能告诉为什么在搜索缓存数据时会发生这种情况吗?转到数据库时不会发生这种情况。数组到 JSON 代码$CategoryResponse = $this->iCategory->All([], []);return \Response::json($CategoryResponse, 200);
1 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
Laravel 的Collections, 和 PHP 中的数组通常可以是关联的,这意味着第一个索引可以是不一定为 0 的东西。当通过Response::json(), or转换为 JSON 时return response->json(),它可以被视为objectJS 中的数组而不是数组。为了解决这个问题,将 转换Collection为一个数组,并indexed通过 PHP 的array_values()函数更改为一个:
$CategoryResponse = $this->iCategory->All([], []);
return response()->json(array_values($CategoryResponse->toArray()), 200);
// Older Laravel syntax
// return \Response::json(array_values($CategoryResponse->toArray()), 200);
在 JSON 响应中,它应该正确显示为具有0第一个索引的数组。
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报
0/150
提交
取消