How To Create AJAX Pagination In Laravel 9

Websolutionstuff | Jan-18-2023 | Categories : Laravel jQuery

In this article, we will see how to create ajax pagination in laravel 9. Here, we will learn how to create jquery ajax pagination in laravel 7, laravel 8, and laravel 9. Also, we will perform without a page refresh data will get. If you use simple pagination then it can reload the page every time. So, it will be time consuming.

Pagination can load chunks of data every time and it can help the webpage for protecting from crashing the page on loading large amounts of data. If you are use jquery ajax pagination then it will more flexible for loading the data without page refresh.

So let's see, ajax pagination in laravel 9, laravel 9 pagination without reload, pagination in laravel 8/9 using ajax, and laravel 9 jquery ajax pagination.

Step 1: Install Laravel 9 Application

Step2: Configure Database

Step 3: Create Route

Step 4: Create Controller

Step 5: Create View

Step 6: Run Laravel 9 Application

 

Step 1: Install Laravel 9 Application

In this step, we will install the laravel 9 application using the following command.

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

 

Step2: Configure Database

Now, we will set up the database configuration to the .env file like database name, user name, and password.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_9_pagination
DB_USERNAME=root
DB_PASSWORD=root

 

 

Step 3: Create Route

In this step, we will add routes to the web.php file. So, add the following code to that file.

<?php
  
use Illuminate\Support\Facades\Route;
  
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::controller(PaginationController::class)->group(function(){
    Route::get('pagination', 'index');
    Route::get('pagination-ajax', 'paginationAjax');
});

 

Step 4: Create Controller

In this step, we will create a PaginationController.php file. So, add the following code to that file.

app/Http/Controllers/PaginationControllers.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;

class PaginationController extends Controller
{
    function index()
    {
        $data = User::paginate(5);
        return view('pagination', compact('data'));
    }

    function paginationAjax(Request $request)
    {
        if($request->ajax())
        {
            $data = User::paginate(5);
            return view('user_pagination_data', compact('data'))->render();
        }
    }
}

?>

 

 

Step 5: Create View

Now, we will create a pagination.blade.php file. So, add the following code to that file. In this file, we will add a user table and create a jquery ajax call for pagination.

resources/views/pagination.blade.php

<!DOCTYPE html>
<html>
	<head>
		<title>How To Create AJAX Pagination In Laravel 9 - Websolutionstuff</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>		
		<div class="container">
			<h3 align="center">How To Create jQuery AJAX Pagination In Laravel 9 - Websolutionstuff</h3>
			<br />
			<div id="user_table">
				@include('user_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_user_data(page);
	    });
	
	    function fetch_user_data(page)
	    {
	        $.ajax({
                url:"/pagination/ajax?page="+page,
                success:function(data)
                {
                    $('#user_table').html(data);
                }
	        });
	    }	 
	});
</script>

resources/views/user_pagination_data.blade.php

<div class="table-responsive">
	<table class="table table-striped table-bordered">
		<tr>
			<th width="10%">ID</th>
			<th width="40%">Name</th>
			<th width="50%">Email</th>
		</tr>
		@foreach($data as $row)
		<tr>
			<td>{{ $row->id }}</td>
			<td>{{ $row->name }}</td>
			<td>{{ $row->email }}</td>
		</tr>
		@endforeach
	</table>
	{!! $data->links() !!}
</div>

 

 

Step 6: Run Laravel 9 Application

Now, we will run the laravel 9 jquery ajax pagination application without reloading using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
Jquery Search Filter Example
Jquery Search Filter Example

In this small post i will so you jquery search filter example. here we will see how to search data using jquery filter....

Read More

Aug-25-2021

How to Convert Datetime into Milliseconds in PHP
How to Convert Datetime into M...

Converting DateTime into milliseconds in PHP can be useful for various applications, such as timestamping events or meas...

Read More

Mar-15-2024

Laravel 9 Datatables Filter with Dropdown
Laravel 9 Datatables Filter wi...

In this article, we will see laravel 9 datatables filter with dropdown. Here we will add datatables...

Read More

Mar-12-2022

Laravel 9 Barcode Generator Example
Laravel 9 Barcode Generator Ex...

In this article, we will see laravel 9 barcode generator example. In this example we will use the milon/barcod...

Read More

Mar-26-2022