panthro
panthro

Reputation: 24061

How to create a paginator?

I've checked out the rather thin docs, but still unsure how to do this.

I have a collection. I wish to manually create a paginator.

I think I have to do something like, in my controller:

new \Illuminate\Pagination\LengthAwarePaginator()

But, what params do I need and do I need to slice the collection? Also how do I then display the 'links' in my view?

Could someone post a simple example how to create a paginator?

Please note, I don't want to paginate eloquent, eg. User::paginate(10);

Upvotes: 0

Views: 482

Answers (2)

Jonathon
Jonathon

Reputation: 16283

Take a look at the Illuminate\Eloquent\Builder::paginate method for an example on how to create one.

A simple example of doing one using an eloquent model to pull out the results etc:

$page = 1; // You could get this from the request using request()->page
$perPage = 15;
$total = Product::count();
$items = Product::take($perPage)->offset(($page - 1) * $perPage)->get();

$paginator = new LengthAwarePaginator(
    $items, $total, $perPage, $page
);
  • The first parameter accepts the results to display on the page that you're on
  • the second is the total number of results (The total number of items you're paginating, not the total number of items you're displaying on that page)
  • the third is the number per page you want to display
  • the fourth is the page that you're on.
  • You can pass in extra options as a fifth parameter if you want to customise things as well.

The links you should just be able to generate using the ->render() or ->links() method on the paginator as you would if you used Model::paginate()


With an existing collection of items you could do this:

$page = 1;
$perPage = 15;
$total = $collection->count();
$items = $collection->slice(($page - 1) * $perPage, $perPage);

$paginator = new LengthAwarePaginator(
    $items, $total, $perPage, $page
);

Upvotes: 2

Saumya Rastogi
Saumya Rastogi

Reputation: 13693

You can create a Paginator like this:

$page = request()->get('page'); // By default LengthAwarePaginator does this automatically.
$collection = collect(...array...);
$total = $collection->count();
$perPage = 10;
$paginatedCollection = new \Illuminate\Pagination\LengthAwarePaginator(
                                    $collection,
                                    $total,
                                    $perPage,
                                    $page
                            );

According to the source code for LengthAwarePaginator (constructor)

public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
{
    foreach ($options as $key => $value) {
        $this->{$key} = $value;
    }
    $this->total = $total;
    $this->perPage = $perPage;
    $this->lastPage = (int) ceil($total / $perPage);
    $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
    $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
    $this->items = $items instanceof Collection ? $items : Collection::make($items);
}

See more about LengthAwarePaginator

To display links in the view:

$paginatedCollection->links();

Hope this helps!

Upvotes: 0

Related Questions