0

我正在使用 Laravel 和 DingoAPI 开发一个 api,它通过分页返回用户的消息线程。

$threads = Thread::forUser($currentUserId)->latest('updated_at')->simplePaginate(1);
return API::response()->array($threads);

我得到了这样的回应:

{
  "per_page": 1,
  "current_page": 1,
  "next_page_url": "http://my.app/api/messages?page=2",
  "prev_page_url": null,
  "from": 1,
  "to": 1,
  "data": [
    {
      "id": 1,
      "subject": null,
      "created_at": "2016-03-18 12:33:38",
      "updated_at": "2016-03-18 12:33:38",
      "deleted_at": null
    }
  ]
}

我该怎么做才能删除响应的某些字段?我只需要数据和 next_page_url ...</p>

我试过这样:

$array_response = [
        'next_page_url' => $threads['next_page_url'],
        'data' => $threads['data']
        ];

但只返回空值。我猜 Dingo Api 正在幕后做一些工作……</p>

4

1 回答 1

0

尝试这个

$threads = Thread::forUser($currentUserId)->latest('updated_at')->simplePaginate(1);
$arrayResponse = json_decode(API::response()->array($threads));
$arrayResponseEdited = [
        'next_page_url' => $arrayResponse['next_page_url'],
        'data' => $arrayResponse['data']
];

return arrayResponseEdited;
于 2016-03-18T22:35:45.230 回答