matttonks11's avatar

Creating a manual Simple Paginator

Hi,

I was wondering if someone can explain how to create a manual simple paginator?

I've got my data, which uses a groupBy statement so I believe I need to create a manual paginator.

I've just got no idea how to do this and from looking around online, there's no simple concrete examples I can find

I think I'm supposed to pass the data to the Illuminate\Pagination\Paginator construct method but where do I go from there in terms of returning my view and displaying the pagination links in that view...


$book->groupBy('isbn')->latest() //my data

Any help would be greatly appreciated

0 likes
4 replies
matttonks11's avatar

@jlrdw thanks for that information, Is there a need for me to use that code if I just want a simple Paginator with the previous and next links?

I took this code from the Laravel up and running book to manually create a paginator, the data gets returned properly, but the links won't display on the first page of results.

If I manually add ?page=2 to the url to get the second page of results, the next page link does not work and the previous page link ignores the current path within my application and returns to app.test?page=2 rather than app.test/books?page=2


public function index(Request $request, Book $book)
    {
        $books = $book->groupBy('isbn')->latest()->get()->toArray();

        $perPage = 16;
        $offsetPages = $request->input('page', 1) - 1;

        $books = array_slice($books, $offsetPages * $perPage, $perPage);

        $books = new Paginator( $books, $perPage);

        return view('books.index', ['books' => $books]);
    }

{{ $books->links() }} //Displaying links in blade view

Do you have any ideas as to what is going wrong?

matttonks11's avatar
Paginator {#12748 ▼
  #hasMore: false
  #items: Collection {#12750 ▶}
  #perPage: 16
  #currentPage: 2
  #path: "https://app.test/books"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:1 [▶]
}

hasMore always seems to be returning false which seems to be why the next link doesn't seem to work

I updated my controller method to this


public function index(Request $request, Book $book)
    {
        $books = $book->groupBy('isbn')->latest()->get()->toArray();

        $perPage = 16;
        $offsetPages = $request->input('page', 1) - 1;

        $books = array_slice($books, $offsetPages * $perPage, $perPage);

        $books = new Paginator( $books, $perPage, Paginator::resolveCurrentPage(), ['path' => Paginator::resolveCurrentPath()]);


        return view('books.index', ['books' => $books]);
    }

jlrdw's avatar

Make a custom template, for example:

https://laracasts.com/discuss/channels/guides/paginator-another-episode

And study the API

https://laravel.com/api/5.7/

https://laravel.com/api/5.7/Illuminate/Contracts/Pagination/LengthAwarePaginator.html

Is there a need for me to use that code if I just want a simple Paginator with the previous and next links?

If you want simple Paginator don't use group by. Could you use order by.

From docs:

Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

Really, in programming a paginator is probably the easiest thing to do, it's just a simple calculation.

Please or to participate in this conversation.