How to create pagination from array in Laravel? (ok)

https://www.itsolutionstuff.com/post/how-to-create-pagination-from-array-in-laravelexample.html

C:\xampp\htdocs\reset\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PaginationController;
/*
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/paginate', [PaginationController::class, 'index']);

C:\xampp\htdocs\reset\app\Http\Controllers\PaginationController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class PaginationController extends Controller {
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function index() {
    $myArray = [
      ['id' => 1, 'title' => 'Laravel CRUD'],
      ['id' => 2, 'title' => 'Laravel Ajax CRUD'],
      ['id' => 3, 'title' => 'Laravel CORS Middleware'],
      ['id' => 4, 'title' => 'Laravel Autocomplete'],
      ['id' => 5, 'title' => 'Laravel Image Upload'],
      ['id' => 6, 'title' => 'Laravel Ajax Request'],
      ['id' => 7, 'title' => 'Laravel Multiple Image Upload'],
      ['id' => 8, 'title' => 'Laravel Ckeditor'],
      ['id' => 9, 'title' => 'Laravel Rest API'],
      ['id' => 10, 'title' => 'Laravel Pagination'],
      ['id' => 11, 'title' => 'Laravel Pagination'],
      ['id' => 12, 'title' => 'Laravel Pagination'],
      ['id' => 13, 'title' => 'Laravel Pagination'],
      ['id' => 14, 'title' => 'Laravel Pagination'],
      ['id' => 15, 'title' => 'Laravel Pagination'],
      ['id' => 16, 'title' => 'Laravel Pagination'],
      ['id' => 17, 'title' => 'Laravel Pagination'],
    ];
    $data = $this->paginate($myArray);
    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);
  }
}

C:\xampp\htdocs\reset\resources\views\paginate.blade.php

@extends('layouts.app')
@section('content')
<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>
    {{ $data->links('pagination::bootstrap-4') }}
</div>
@endsection

How to create pagination from array in Laravel?

We will create our custom collection object with array and create pagination using laravel eloquent. we will use Paginator and LengthAwarePaginator facade to creating pagination from custom array in laravel 6, laravel 7, laravel 8 and laravel 9.

In this example, we will simple create one route and call controller method. that controller method we will create our custom array and convert into collection object. we also create one paginate() in same controller to create pagination in laravel. then you have to just call view and pass result variable. you can use like your paginate object.

Create Route

In next step, we will add new one route in web.php file. route we will call controller method So let's simply create both route as bellow listed:

routes/web.php

Route::get('paginate', 'PaginationController@index');

Create Controller

Here, we will create PaginationController with two method, one for call route and another for creating custom pagination. So let's add controller as like bellow:

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 CRUD'],            ['id'=>2, 'title'=>'Laravel Ajax CRUD'],            ['id'=>3, 'title'=>'Laravel CORS Middleware'],            ['id'=>4, 'title'=>'Laravel Autocomplete'],            ['id'=>5, 'title'=>'Laravel Image Upload'],            ['id'=>6, 'title'=>'Laravel Ajax Request'],            ['id'=>7, 'title'=>'Laravel Multiple Image Upload'],            ['id'=>8, 'title'=>'Laravel Ckeditor'],            ['id'=>9, 'title'=>'Laravel Rest API'],            ['id'=>10, 'title'=>'Laravel Pagination'],        ];          $data = $this->paginate($myArray);           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 need to create blade file to print data. so let's create simple blade file as like bellow:

app/Http/Controllers/PaginationController.php

Read Also: Laravel Wherein Query Example

<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 it can help you...

Last updated