0

i'm developing an api with Laravel and DingoAPI which returns the message threads of the user with pagination.

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

and i get this kind of response :

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

What can i do to remove some field of the response ? i just need data and next_page_url …

i tried this way :

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

but only null values are returned. I guess Dingo Api is doing some work behind the scene…

Mushu8
  • 173
  • 1
  • 13

1 Answers1

0

Try this

$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;
Can Celik
  • 2,050
  • 21
  • 30