php - laravel mongo db在分页中获取数据时间作为日期
问题描述
当我想获得嵌入流量模型时,API 响应为嵌入模型发布错误的日期格式,并protected $dates = ['updated_at','created_at'];
在我的流量模型中使用但不起作用并且响应仍然发出错误的日期和 ID 格式
注意:当我使用get()
而不是simplePaginate()
或paginate()
查询生成器时发出正确的 json 格式!!!!!!
分页 API 响应:
{
"current_page": 1,
"data": [
{
"lat": 59.56555555455,
"lon": 35.54598994564,
"type": "manual",
"updated_at": {
"$date": {
"$numberLong": "1540712586000"
}
},
"created_at": {
"$date": {
"$numberLong": "1540712586000"
}
},
"_id": {
"$oid": "5bd5688a6925020c34006773"
}
}//.....
]
}
get() API 响应:
[
{
"lat": 59.56555555455,
"lon": 35.54598994564,
"type": "manual",
"updated_at": "2018-10-28 07:43:06",
"created_at": "2018-10-28 07:43:06",
"_id": "5bd5688a6925020c34006773"
},
{
"lat": 59.56555555455,
"lon": 35.54598994567,
"type": "system",
"updated_at": "2018-10-28 07:43:06",
"created_at": "2018-10-28 07:43:06",
"_id": "5bd5688a6925020c34006774"
}/...
]
代码
$userTrafic = Auth::user()->userTraffic()->firstOrFail();
if ($userTrafic) {
return $userTrafic->traffics()->orderBy('updated_at', 'desc')->simplePaginate(20); //or get()
}
else return response(['status' => false, 'message' => 'traffic\'s user not found'], 404);
解决方案
只需更改库:Jensegers\Mongodb\Relations\EmbedsMany.php
现在在方法中添加$sliced = $this->toCollection($sliced);
到第 294 行(返回之前),paginate()
您可以获得如下结果:
{
"current_page": 2,
"data": [
{
"lat": 59.56555555455,
"lon": 35.54598994564,
"type": "system",
"updated_at": "2018-10-28 08:12:36",
"created_at": "2018-10-28 08:12:36",
"_id": "5bd56f746925020c34006778"
},
{
"lat": 59.56555555455,
"lon": 35.54598994564,
"type": "manual",
"updated_at": "2018-10-28 08:13:55",
"created_at": "2018-10-28 08:13:55",
"_id": "5bd56fc36925020c34006779"
},
{
"lat": 59.56555555455,
"lon": 35.54598994567,
"type": "system",
"updated_at": "2018-10-28 08:13:55",
"created_at": "2018-10-28 08:13:55",
"_id": "5bd56fc36925020c3400677a"
},
{
"lat": 59.56555555455,
"lon": 35.54598994564,
"type": "system",
"updated_at": "2018-10-28 08:13:55",
"created_at": "2018-10-28 08:13:55",
"_id": "5bd56fc36925020c3400677b"
},
{
"lat": 59.56555555455,
"lon": 35.54598994564,
"type": "manual",
"updated_at": "2018-10-28 10:39:46",
"created_at": "2018-10-28 10:39:46",
"_id": "5bd591f26925020c3400677d"
}
],
"first_page_url": "**********traffic/list?page=1",
"from": 6,
"last_page": 3,
"last_page_url": "**********traffic/list?page=3",
"next_page_url": "**********traffic/list?page=3",
"path": "**********traffic/list",
"per_page": "5",
"prev_page_url": "**********traffic/list?page=1",
"to": 10,
"total": 12
}
推荐阅读
- javascript - 如何让我的 Excel 表在每个月底将数据复制到另一张表?
- regex - 正则表达式仅匹配第一个字符
- jquery - 如何获取动态创建的锚标签以重定向到操作方法
- spring-boot - EL1008E: 在“...用户”类型的对象上找不到属性或字段“用户名”- 可能不是公共的或无效的?
- html - 我们如何使用带有可自定义控件的 ngFor formArrays?
- android - 当应用程序被系统杀死/重新启动时,Android 服务崩溃
- unity3d - 更快的编译 - 资产商店插件的编辑器文件夹
- swift - 快速实现 socket.io-client
- amazon-web-services - 为什么 EC2 实例的内存利用率不是 Amazon CloudWatch 的默认指标?
- javascript - 用于现代浏览器的 document.registerElement