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
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
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
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');
});
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();
}
}
}
?>
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>
Now, we will run the laravel 9 jquery ajax pagination application without reloading using the following command.
php artisan serve
You might also like:
In this article, we will see how to toggle between dark and light modes using jquery. As per the current trend of web de...
Nov-24-2020
In this article, we will see laravel 9 socialite login with a facebook account. Many websites provide different typ...
Apr-16-2022
In this example, we will see how to convert an image into a base64 string using jquery. Base64 encoding schemes are...
Jan-03-2022
In this post i will show you laravel 8 user role and permission with example, here we will see how to set user role and...
Jun-02-2021