TomCawthorn's avatar

Pagination with Group By not working

I know from the docs that when paginating with a query that contains Group By isn't going to work.

I'm struggling with the alternative, custom method.

So the Paginator class is initialised with:


<?php

public function __construct($items, $perPage, $currentPage = null, array $options = [])

I'm calling the object like this for now


<?php

return new \Illuminate\Pagination\Paginator($query->get(), 20);

And it's not quite working. I'm passing in a collection of all the results, and expecting a paginated list back.

However, whatever page I pass into the paginator two things happen.

  1. I always have the first page returned
  2. The page numbers don't show in from the render method

It would seem that the page number is not being taken into account in this method:


<?php

    /**
     * Check for more pages. The last item will be sliced off.
     *
     * @return void
     */
    protected function checkForMorePages()
    {
        $this->hasMore = count($this->items) > ($this->perPage);

        $this->items = $this->items->slice(0, $this->perPage);
    }

I've tracked down the the 'throwing away of the rest of my collection' problem to this method, particuarly the slice.

Any ideas? :(

Thanks!

0 likes
1 reply
TomCawthorn's avatar

Okay, I've just solved problem 2 from the master docs.

See "Creating A Paginator Manually" here http://laravel.com/docs/master/pagination.

I've now got the numbers appearing via the render method.

However, it would seem 'per page' is being totally ignored - I'm getting all the results back and page number makes no difference!

The construct for the LengthAwarePaginator accepts:


<?php

    public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])

and I'm sending in:


<?php

            $results; // Collection of 7 items
            $results->count(); // 7 
            $howMany; // 2

        return new \Illuminate\Pagination\LengthAwarePaginator($results, $results->count(), $howMany);

I get 4 pages available from the render method, but no pagination

Please or to participate in this conversation.