Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Spec Pagination Support #31

Closed
harlan-zw opened this issue Nov 14, 2018 · 5 comments
Closed

JSON Spec Pagination Support #31

harlan-zw opened this issue Nov 14, 2018 · 5 comments

Comments

@harlan-zw
Copy link

Hi, great package really good 👍 Came across something that may be worth supporting.

The https://github.com/spatie/laravel-query-builder package has an alternative way of setting up the pagination where you install https://github.com/spatie/laravel-json-api-paginate and the pagination adheres to the JSON API spec.

The pagination supported in this plugin won't handle this out of the box, you can get around it by providing the custom params.

params({ 'page[number]': page, 'page[limit]': limit })

Ideally there would be a toggle to switch to this mode or some way to globally customize the param names used by the Parser to handle it.

@robsontenorio
Copy link
Owner

Right now vue-api-query provides pagination.

See here https://github.com/robsontenorio/vue-api-query#pagination

// GET /users?sort=firstname&page=1&limit=20

let users = await User        
        .orderBy('firstname')
        .page(1) 
        .limit(20)
        .get()

If you are using Laravel, there is a full support for pagination out-of-the-box by calling paginate() method on your Eloquent queries, eg User::paginate() (you can pass parameters to this method)

https://laravel.com/docs/5.7/pagination

We have all necessary info to paginate here:

// sample of response  for paginate
{
   "total": 50,
   "per_page": 15,
   "current_page": 1,
   "last_page": 4,
   "first_page_url": "http://laravel.app?page=1",
   "last_page_url": "http://laravel.app?page=4",
   "next_page_url": "http://laravel.app?page=2",
   "prev_page_url": null,
   "path": "http://laravel.app",
   "from": 1,
   "to": 15
}

What would be advantages to use JSON API SPEC for pagination? Once this package does not follow this spec?

@robsontenorio
Copy link
Owner

For now, lets stick to default. If you can, please submit a PR.

@docmattman
Copy link

docmattman commented Apr 13, 2020

@loonpwn, not sure if you're still looking, but to accomplish this, I added the following to my base Model:

  limit(val) {
      this.paginate({ size: val })
      return this
  }

  page(val) {
    this.paginate({ number: val })
    return this
  }

  paginate(params) {
    const existing =
      this._builder.payload && this._builder.payload.page
        ? this._builder.payload.page
        : {}
    if (this._builder.pageValue) existing.number = this._builder.pageValue
    if (this._builder.limitValue) existing.size = this._builder.limitValue
    this._builder.payload = {
      ...this._builder.payload,
      page: {
        ...existing,
        ...params
      }
    }
    return this
  }

Then, you can use the limit and page model methods, or just set both using:

MyModel.paginate({ size: 25, number: 5 })...

@lordroseman
Copy link

@loonpwn, not sure if you're still looking, but to accomplish this, I added the following to my base Model:

  limit(val) {
      this.paginate({ size: val })
      return this
  }

  page(val) {
    this.paginate({ number: val })
    return this
  }

  paginate(params) {
    const existing =
      this._builder.payload && this._builder.payload.page
        ? this._builder.payload.page
        : {}
    if (this._builder.pageValue) existing.number = this._builder.pageValue
    if (this._builder.limitValue) existing.size = this._builder.limitValue
    this._builder.payload = {
      ...this._builder.payload,
      page: {
        ...existing,
        ...params
      }
    }
    return this
  }

Then, you can use the limit and page model methods, or just set both using:

MyModel.paginate({ size: 25, number: 5 })...

cool! thanks!

@AdrienPoupa
Copy link

Since I did not see it in the readme, this is how you would do it with the merged PR and laravel-json-api-paginate

  parameterNames () {
    return {
      include: 'include',
      filter: 'filter',
      sort: 'sort',
      fields: 'fields',
      append: 'append',
      page: 'page[number]',
      limit: 'page[size]'
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants