samedi 16 février 2019

Why JSON structure of default pagination response and eloquent pagination resource are different?

I'm developing an API, and for index routes, I return paginated but now I should using eloquent resources, but pagination JSON structure of these two are different and front-end code doesn't work anymore.

my codes are:

// AdminUserController.php
public function index()
{
    return User::paginate();
}

// ClientUserController.php
public function index()
{
     return new UserCollection(User::paginate());
}

the first JSON structure like :

{
  "total": 50,
  "per_page": 15,
  "current_page": 1,
  "last_page": 4,
  "next_page_url": "http://my.app/api/admin/users?page=2",
  "prev_page_url": null,
  "from": 1,
  "to": 15,
  "data": [
    {
      // Result Object
    },
    {
      // Result Object
    }
  ]
}

But the second is like:

{
    "data": [
        {
            // Result Object
        },
        {
            // Result Object
        }
    ],
    "links":{
        "first": "http://my.app/api/app/users?page=1",
        "last": "http://my.app/api/app/users?page=2",
        "prev": null,
        "next": null
    },
    "meta":{
        "current_page": 1,
        "from": 1,
        "last_page": 2,
        "path": "http://my.app/api/admin/users",
        "per_page": 15,
        "to": 15,
        "total": 25
    }
}

why laravel paginated responses have different JSON structure?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2BDUSuZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire