Auto Load pagination on Page Scroll using AJAX in Laravel 10

Spread the love

Hello Guys,

Discover How to Implement Automatic Pagination in Laravel 10 with AJAX

In this tutorial, we will focus on achieving automatic pagination in Laravel 10 as you scroll down the page. We’ll leverage AJAX to seamlessly load additional data. This guide will walk you through the process of creating a Laravel 10 Auto Load pagination on Page Scroll.

To display posts and retrieve data, we’ll start by creating a new controller, PostController, with an index method. Follow the steps below to add the necessary code:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
     * Write code on Method
     * @return response()
    public function index(Request $request)
        $posts = Post::paginate(10);
        if ($request->ajax()) {
            $view = view('data', compact('posts'))->render();
            return response()->json(['html' => $view]);
        return view('posts', compact('posts'));

In the final phase, to showcase the list of posts and incorporate the following code, create posts.blade.php and data.blade.php.


Also Read: How to use One to One Relationship in LARAVEL 10

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Laravel Load More on Scroll Example -</title>
    <link rel="stylesheet" href="">
<div class="container mt-5" style="max-width: 750px">
    <h1>Laravel Load More on Scroll Example -</h1>
    <div id="data-wrapper">
    <!-- Data Loader -->
    <div class="auto-load text-center" style="display: none;">
        <svg version="1.1" id="L9" xmlns="" xmlns:xlink=""
            x="0px" y="0px" height="60" viewBox="0 0 100 100" enable-background="new 0 0 0 0" xml:space="preserve">
            <path fill="#000"
                d="M73,50c0-12.7-10.3-23-23-23S27,37.3,27,50 M30.9,50c0-10.5,8.5-19.1,19.1-19.1S69.1,39.5,69.1,50">
                <animateTransform attributeName="transform" attributeType="XML" type="rotate" dur="1s"
                    from="0 50 50" to="360 50 50" repeatCount="indefinite" />
<script src=""></script>
    var ENDPOINT = "{{ route('posts.index') }}";
    var page = 1;
    Call on Scroll
    $(window).scroll(function () {
        if ($(window).scrollTop() + $(window).height() >= ($(document).height() - 20)) {
    call infinteLoadMore()
    function infinteLoadMore(page) {
                url: ENDPOINT + "?page=" + page,
                datatype: "html",
                type: "get",
                beforeSend: function () {
            .done(function (response) {
                if (response.html == '') {
                    $('.auto-load').html("We don't have more data to display :(");
            .fail(function (jqXHR, ajaxOptions, thrownError) {
                console.log('Server error occured');

Also Read: How to use Drag and Drop Datatable Rows for Sorting using laravel 10

@foreach ($posts as $post)
<div class="card mb-2"> 
    <div class="card-body">{{ $post->id }} 
        <h5 class="card-title">{{ $post->title }}</h5>
        {!! $post->body !!}

Explore our Laravel blog section for insightful content and helpful tips.

Leave a Comment