laravel mongo db在分页中获取数据时间作为日期

时间:2018-10-28 09:01:40

标签: php mongodb laravel

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

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

数据库: enter image description here

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

1 个答案:

答案 0 :(得分:0)

只需更改库: Jenssegers \ Mongodb \ Relations \ EmbedsMany.php

$sliced = $this->toCollection($sliced);方法中将paginate()添加到第294行(返回之前) 现在您可以得到如下结果:

{
"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
}