How to implement infinite ajax scroll pagination in Laravel?

How to implement infinite ajax scroll pagination in Laravel?

In this post we will give you information about How to implement infinite ajax scroll pagination in Laravel?. Hear we will give you detail about How to implement infinite ajax scroll pagination in Laravel?And how to use it also give you demo for it if it is necessary.

We always want to see auto load more data on page scroll on your home page or other pages for posts, news, listing etc like on facebook, twitter, linkedin etc. If you implemented infinite scroll on your php application then you don’t require to give pagination and also not require to page refresh every time when page scroll.

So, Today, i going to give you simple example of infinite scroll using jquery ajax on page scroll in laravel application from scratch. I use jquery scroll event for page scroll. this example is pretty simple, you can customize simply for your development.

You have to just follow bellow few step and you will get infinite scroll in your laravel application. After finish all those step you will be find output as like bellow preview:


Step 1: Add Table and Model

we require to create new table “posts” that way we will get data from this table, you can use your own table but this is for example. we have to create migration for posts table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_post_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 posts table.

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class CreatePostTable extends Migration



* Run the migrations.


* @return void


public function up()


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








* Reverse the migrations.


* @return void


public function down()





Ok, now we have to run migration using laravel artisan command:

php artisan migrate

Now, we require to create table model for posts table, so fist create new Post.php in your app directory as like bellow:


namespace App;

use IlluminateDatabaseEloquentModel;

class Post extends Model


public $fillable = ['title','description'];


Step 2: Add Route

In this is step we need to add route for generate view. so open your app/Http/routes.php file and add following route.

Route::get('my-post', 'PostController@myPost');

Also see:PHP – File upload progress bar with percentage using form jquery example

Step 3: Create Controller

If you haven’t PostController then we should create new controller as PostController in this path app/Http/Controllers/PostController.php. Make sure you should have posts table with some data. this controller will manage data and view file, so put bellow content in controller file:


namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppHttpRequests;

use AppPost;

class PostController extends Controller


public function myPost(Request $request)


$posts = Post::paginate(5);

if ($request->ajax()) {

$view = view('data',compact('posts'))->render();

return response()->json(['html'=>$view]);


return view('my-post',compact('posts'));



Step 4: Create View Files

In last step, we have to create view two file “my-post.blade.php” for main view and another for data, so first create my-post.blade.php file:


<!DOCTYPE html>



<title>Laravel infinite scroll pagination</title>

<script src=""></script>

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

<style type="text/css">


background: #e1e1e1;

padding: 10px 0px;

width: 100%;





<div >

<h2 >Laravel infinite scroll pagination</h2>


<div id="post-data">




<div style="display:none">

<p><img src="http://demo.onlinecode/plugin/loader.gif">Loading More post</p>


<script type="text/javascript">

var page = 1;

$(window).scroll(function() {

if($(window).scrollTop() + $(window).height() >= $(document).height()) {





function loadMoreData(page){



url: '?page=' + page,

type: "get",

beforeSend: function()







if(data.html == " "){

$('.ajax-load').html("No more records found");






.fail(function(jqXHR, ajaxOptions, thrownError)


alert('server not responding...');







Also see:How to make simple dependent dropdown using jquery ajax in Laravel 5?

@foreach($posts as $post)


<h3><a href="">{{ $post->title }}</a></h3>

<p>{{ str_limit($post->description, 400) }}</p>

<div >

<button >Read More</button>


<hr style="margin-top:5px;">



Ok, now you can check and test…..

Hope this code and post will helped you for implement How to implement infinite ajax scroll pagination in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

43  +    =  47

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US