MehulBawadia's avatar

Manual Pagination in Laravel 5.1

How to create a manual pagination in Laravel 5.1 ?

I have an array of results (more than 25 result) fetched from the database. I am trying to implement Laravel Pagination in my application.

What I want is paginate the results fetched.

What I get is pagination of the same results.

The code that I have used so far:

<?php
$productsInParent = $category->products;
$productsInParentCategory = new Illuminate\Support\Collection;
$allProducts = $allProductsFromChild = [];

foreach($productsInParent as $prodInParent) {
    $allProducts[] = $prodInParent;
}

foreach($category->childs as $child) {
    foreach($child->products as $productsInChildren) {
        $allProductsFromChild[] = $productsInChildren;
    }
}

$fetchAllProducts = array_merge($allProducts, $allProductsFromChild);

$result = array_slice($fetchAllProducts, 0, 12);

$paginateResults = new Illuminate\Pagination\LengthAwarePaginator($result, count($fetchAllProducts), 12);
?>

I do get the pagination, but when working inspecting, on page 2, on page 3, and so on.. I get the result that is visible on page 1 of the pagination on all the subsequent pages of the pagination.

Kindly help me out with this. Any help is highly appreciated.

P.S.: I am trying to learn on my own how to integrate custom pagination in Laravel 5.1

0 likes
4 replies
jrean's avatar
jrean
Best Answer
Level 8

@IamCrazyD you can do something like that into a controller or anywhere you want to use it...

use Illuminate\Pagination\LengthAwarePaginator;

/**
     * Create a length aware custom paginator instance.
     *
     * @param  Collection  $items
     * @param  int  $perPage
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    protected function paginate($items, $perPage = 12)
    {
        //Get current page form url e.g. &page=1
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        //Slice the collection to get the items to display in current page
        $currentPageItems = $items->slice(($currentPage - 1) * $perPage, $perPage);

        //Create our paginator and pass it to the view
        return new LengthAwarePaginator($currentPageItems, count($items), $perPage);
    }


    public function index()
{
        $articles = $this
            ->paginate($this->foo->getArticles())
            ->setPath('articles');
}

It's just to give you the idea.

1 like
jlrdw's avatar

Custom pagination has got to be the simplest easiest thing in the universe please Google it.

dmsone97's avatar

@jrean

That partially works but its missing the path. When I tried using it it always paginated the root path so my url would look something like whatever.com/?page=2 when I wanted whatever.com/blog?page=2. To add the desired path of the page you're paginating on add this:

$paginate = new LengthAwarePaginator($currentPageItems, count($items), $perPage);
// set url
$paginate = $paginate->setPath(Request::url());
return $paginate;
jrean's avatar

@dmsone97 it was already mentioned into my comment:

public function index()
{
        $articles = $this
            ->paginate($this->foo->getArticles())
            ->setPath('articles');
}

But of course you can directly apply into the custom paginate() method. Your taste, your way :-)

Please or to participate in this conversation.