2

I'm dealing now with pagination of search results in my API made in Laravel and I came across this in the documentation:

https://laravel.com/docs/5.3/pagination#converting-results-to-json

It seems nice but at the same time I'm quite concerned about the fact that it alters the structure of the result, since it hides the returned data inside the "data" key.

The problem is, until now I was returning a non-paginated JSON-encoded array of objects and including pagination this way would either require making remarkable modifications in the front-end or it would bring inconsitency across the API (there would be API calls which return the data nested and other ones which return them directly - I don't like it).

I would probably like more to include the pagination metadata in the header of the response which won't mess up my current structure of responses. Do you know about any out-of-the-box solution like this (ideally a Laravel package)? Or what other approach would you recommend me to resolve my dilemma?

3 Answers 3

3

There seems to be two options for pagination information in an API - in the response body as Laravel does, and in the header information (like github does).

I am having this issue myself with Laravel with a new project, and I have pretty much decided that I want to do pagination in headers - it just feels a bit more right (admittedly, this is a hotly debated topic).

I haven't yet built this, so may change my mind as time goes on, but building a piece of middleware and attaching it to the API group seems to be the way to go.

In that middleware, we need to get the full response before you add the headers, something like:

public function handle($request, Closure $next, $guard = null)
{
    // let other middleware handle the request first
    $response = $next($request);

    // get paging links here
    // <url>; rel="next", <url>; rel="prev", <url>; rel="first", <url>; rel="last"
    $response->header("Link: " . $links);

    return $response;
}

The Laravel docs on middleware are recommended reading too.

Sign up to request clarification or add additional context in comments.

Comments

0

Tip: Never stick to a mistake or you'll be doomed to hell, believe me.

By the above sentence mean that your frontend is a problem, not the laravel response, I've been using this pagination in many apps and these always worked for me, mean this page_data and page_meta_data are served in a single object because these belongs togather.

I want to show you two examples

Instagram Instagram Post Pagination

Airbnb enter image description here

I didn't mean to say that these are standards, but still you can think that this is the best approach.

Laravel Package

I'm almost 100% sure that there is no laravel package for you to help.

Hack/Fix for your issue

Laravel gives you freedom of adding headers to a response all along.

return response()->headers($key, $value)->headers($key, $value);

Disclaimer: This is not the right way to do this, rather this is not even a close to the right way.

2 Comments

This does not answer the question on how to put pagination data in headers. Also, there are numerous (counter) examples of APIs that return pagination data in headers.
Hi @bzeaman I think I already added this headers pagination thingy return response()->headers($key, $value)->headers($key, $value);
0

I have the same issue. I like link header pagination, github uses it, since response body contains only the "raw" data and not meta data.

I think this Laravel middleware example can help you.

Route::get('items', 'ItemController@index')->middleware('link-header-pagination');

https://github.com/y-zono/laravel-link-header-pagination

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.