Laravel Pagination with array or object

Sharad Jaiswal
Written by Sharad Jaiswal

Posted On: Nov 07, 2018

 

You have used Laravel Pagination with Eloquent results. Sometimes we have to do paginations in Laravel on arrays or custom objects. In this tutorial, we will see how to do Laravel manual pagination.

Here we are going to use LengthAwarePaginator class of Laravel to create custom pagination in Laravel.

Here is code to implement custom Pagination in Laravel.

//In Controller

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
 
use App\Http\Requests;
 
class productsController extends Controller
{
     public function products(Request $request)
    {
        $products = [
            'product1',
            'product2',
            'product3',
            'product4',
            'product5',
            'product6',
            'product7',
            'product8',
            'product9',
            'product10'
            ];
 
        // Get current page form url e.x. &page=1
        $currentPage = LengthAwarePaginator::resolveCurrentPage();
 
        // Create a new Laravel collection from the array data
        $productCollection = collect($products);
 
        // Define how many products we want to be visible in each page
        $perPage = 1;
 
        // Slice the collection to get the products to display in current page
        $currentPageproducts = $productCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();
 
        // Create our paginator and pass it to the view
        $paginatedproducts= new LengthAwarePaginator($currentPageproducts , count($productCollection), $perPage);
 
        // set url path for generted links
        $paginatedproducts->setPath($request->url());
 
        return view('products_view', ['products' => $paginatedproducts]);
    }
 
}

Also Read: Laravel Pagination with Ajax

// In your view

<h1>Products List</h1>
<pre> 
<ul>
@foreach ($products as $product) 
   <li> {{ $product }} </li>
@endforeach
</ul>
 
<div>
{{ $products->links() }}
</div>

//In your Routes

Route::get('products','ProductsController@items');

    Please Login or Register to leave a response.

    Related Articles

    Laravel Tutorials

    Laravel 5.5 Middleware complete Tutorial

    In Laravel, you can think middleware as a system or tool that are used to filter all HTTP requests entering your application. Middleware works between request and response of our application. It sits ..

    Laravel Tutorials

    Laravel Pagination Ajax

    Creating Ajax based pagination is Laravel 5.6. Ajax (Asynchronous JavaScript and XML) is a technique to update a specific part of webpage asynchronously without reloading the page. In this article, we..

    Laravel Tutorials

    Laravel Pagination

    According to Wikipedia Pagination is a process of separating or dividing a document or digital contents into discrete pages. In Core PHP and other frameworks, paginating record is a painful task. Lara..

    Ask a Question