Laravel 5.7 Ajax Pagination Example

By Hardik Savani November 5, 2023 Category : PHP Laravel Bootstrap jQuery JSON Ajax

In this tutorial, I would like to show you how to create jquery ajax pagination in laravel 5.7. we will create a step by step simple ajax pagination in laravel 5.7.

Pagination is a very basic requirement of every project like Admin Panel, ERP or back-end Panel. A pagination helps us to load few records every time, that way cannot broken web page due to lots of data. If you are making pagination and do it using ajax then it a better way. Ajax Pagination load only your table data instead of the whole page. So ajax pagination is very helpful.

In this example, we simply create "items" table using migration command and add some dummy records. After that, we will create one route for display view and write code jquery ajax on blade file. So, you have to just follow below step and you will get simply ajax pagination in laravel 5.7.

Step 1 : Install Laravel 5.7

we are going from scratch, So we require to get fresh Laravel 5.7 application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Table and Model

In this step we have to create migration for items table using Laravel 5.7 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path "database/migrations" and you have to put bellow code in your migration file for create items table.


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateItemsTable extends Migration



* Run the migrations.


* @return void


public function up()


Schema::create('items', function (Blueprint $table) {







* Reverse the migrations.


* @return void


public function down()





Then after, simply run migration:

php artisan migrate

After create "items" table you should create Item model for items, so first create file in this path "app/Item.php" and put bellow content in item.php file:



namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model



Step 3: Create Route

In this is step we need to create routes for items listing. so open your "routes/web.php" file and add following route.



Step 4: Create Controller

In this step, we have to create two view file one for layout and another for data. now we should create new controller as AjaxController in this path "app/Http/Controllers/AjaxController.php". this controller will manage all listing items and item ajax request and return response, so put bellow content in controller file:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Item;

class AjaxController extends Controller



* Display a listing of the resource.


* @return \Illuminate\Http\Response


public function ajaxPagination(Request $request)


$data = Item::paginate(5);

if ($request->ajax()) {

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


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



Step 5: Create Blade Files

In Last step, let's create ajaxPagination.blade.php(resources/views/ajaxPagination.blade.php) for layout and lists all items code here and put following code:


<!DOCTYPE html>



<title>Laravel 5.7 AJAX Pagination Example -</title>

<link rel="stylesheet" href="" />

<script src="//"></script>



<div class="container">

<h1>Laravel 5.7 AJAX Pagination Example -</h1>

<div id="tag_container">




<script type="text/javascript">

$(window).on('hashchange', function() {

if (window.location.hash) {

var page = window.location.hash.replace('#', '');

if (page == Number.NaN || page <= 0) {

return false;








$(document).on('click', '.pagination a',function(event)





var myurl = $(this).attr('href');

var page=$(this).attr('href').split('page=')[1];




function getData(page){



url: '?page=' + page,

type: "get",

datatype: "html"



location.hash = page;

}).fail(function(jqXHR, ajaxOptions, thrownError){

alert('No response from server');







<table class="table table-bordered">



<th width="100px">ID</th>





@foreach ($data as $value)


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

<td>{{ $value->name }}</td>





{!! $data->render() !!}

Make sure you have some dummy data on your items table before run this example. Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:


I hope it can help you...