首页 > 解决方案 > laravel mongo db在分页中获取数据时间作为日期

问题描述

当我想获得嵌入流量模型时,API 响应为嵌入模型发布错误的日期格式,并protected $dates = ['updated_at','created_at']; 在我的流量模型中使用但不起作用并且响应仍然发出错误的日期和 ID 格式

注意:当我使用get()而不是simplePaginate()paginate()查询生成器时发出正确的 json 格式!!!!!!

D b: 在此处输入图像描述

分页 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);

标签: phpmongodblaravel

解决方案


只需更改库: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
}

推荐阅读