custom pagination laravel code example

Example 1: laravel pagination

// in, AppServiceProvider -> boot() - set: 
Paginator::useBootstrap();  // for compatibility with bootstrap

// in controller 
$storage = Model::paginate(10);

// in view 
{!! $storage->links() !!}

Example 2: laravel pagination

{{ $users->withQueryString()->links() }}

Example 3: create custom pagination in laravel 7 for api

<?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);
    }
}

# Blade file
<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() }}

Example 4: laravel pagination

DB::table('users') -> paginate(15)

Example 5: laravel pagination ajax example

<!DOCTYPE html>
<html>
 <head>
  <title>Laravel Pagination using Ajax</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style type="text/css">
   .box{
    width:600px;
    margin:0 auto;
   }
  </style>
 </head>
 <body>
  <br />
  <div class="container">
   <h3 align="center">Laravel Pagination using Ajax</h3><br />
   <div id="table_data">
    @include('pagination_data')
   </div>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){

 $(document).on('click', '.pagination a', function(event){
  event.preventDefault(); 
  var page = $(this).attr('href').split('page=')[1];
  fetch_data(page);
 });

 function fetch_data(page)
 {
  $.ajax({
   url:"/pagination/fetch_data?page="+page,
   success:function(data)
   {
    $('#table_data').html(data);
   }
  });
 }
 
});
</script>

Example 6: custom pagination laravel css

php artisan vendor:publish --tag=laravel-pagination
  OR
use Illuminate\Pagination\Paginator;

public function boot()
{
    Paginator::defaultView('your-pagination-view-name');

}