Friday, February 21, 2020

How to paginate Laravel collection ?


How to paginate Laravel collection ?

In this tutorial, I want to show you how to create a pagination from an array or collection object in the Laravel 6 application. We create a Laravel 6 pagination from the matrix example. This example helps create a paginated collection object in Laravel 6.

We will create our custom collection object with matrix and create a pagination with eloquent laravel. We will use the Paginator and LengthAwarePaginator facade to create a pagination from a custom matrix in Laravel 6.

In this example, we simply create a route and call the controller method. This controller method creates our custom matrix and makes it a collection object. We also create a paginate () on the same controller to create a pagination in Laravel 6. Then all you have to do is call view and pass the result variable. You can use as a paginated object.

So let's take a look at the following example step by step.

Create Route


In the next step we add a new path to the web.php file. route we call the controller method So we just create both routes as described below:

routes/web.php
Route::get('paginate', 'PaginationController@index');


Create Controller


Here we create PaginationController using two methods, one for the call route and the other for creating a custom page. Let's add the controller as follows:

app/Http/Controllers/PaginationController.php
<?php

  

namespace App\Http\Controllers;

  

use Illuminate\Http\Request;

use Illuminate\Pagination\Paginator;

use Illuminate\Support\Collection;

use Illuminate\Pagination\LengthAwarePaginator;

  

class PaginationController extends Controller

{

    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    public function index()

    {

        $myArray = [

            ['id'=>1, 'title'=>'Laravel 6 CRUD'],

            ['id'=>2, 'title'=>'Laravel 6 Ajax CRUD'],

            ['id'=>3, 'title'=>'Laravel 6 CORS Middleware'],

            ['id'=>4, 'title'=>'Laravel 6 Autocomplete'],

            ['id'=>5, 'title'=>'Laravel 6 Image Upload'],

            ['id'=>6, 'title'=>'Laravel 6 Ajax Request'],

            ['id'=>7, 'title'=>'Laravel 6 Multiple Image Upload'],

            ['id'=>8, 'title'=>'Laravel 6 Ckeditor'],

            ['id'=>9, 'title'=>'Laravel 6 Rest API'],

            ['id'=>10, 'title'=>'Laravel 6 Pagination'],

        ];

  

        $myCollectionObj = collect($myArray);

  

        $data = $this->paginate($myCollectionObj);

   

        return view('paginate', compact('data'));

    }

   

    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    public function paginate($items, $perPage = 5, $page = null, $options = [])

    {

        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

        $items = $items instanceof Collection ? $items : Collection::make($items);

        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);

    }

}


Create View File


Here we just have to create a blade file to print data. Therefore, we create a simple blade file like the following:

app/Http/Controllers/PaginationController.php
<div class="container">

    <table class="table table-bordered">

        <tr>

            <th>Id</th>

            <th>Title</th>

        </tr>

        @foreach($data as $post)

        <tr>

            <td>{{ $post->id }}</td>

            <td>{{ $post->title }}</td>

        </tr>

        @endforeach

    </table>

</div>

   

{{ $data->links() }}


Now you can run and check.

I hope this could help you ...


1 comment:

Please don't enter any spam link in the comment box.