Step 1 : Install Laravel using the following command.
composer create-project --prefer-dist laravel/laravel Laravel
php artisan make:auth
php artisan make:controller HomeController
Route::get('load_users', 'HomeController@load_users');
@extends('layouts.app') @section('content') <div class="col-md-12 table_center"> <div class="col-md-10"> <h1 class="text-center">Users Table</h1> <table class="table table-striped"> <thead> <th>ID</th> <th>Name</th> <th>Email</th> </thead> <tbody> @if(count($users) > 0) @foreach($users as $user) <tr> <td>{{$user->id}}</td> <td>{{$user->name}}</td> <td>{{$user->email}}</td> </tr> @endforeach <tr id="remove-row"> <td></td> <td></td> <td> <img src="{{asset('images/Loading_icon.gif')}}" style="width: 100px;height: 80px"> <input type="hidden" value="{{$user->id}}" id="user_id"> </td> </tr> @else <h1>No Users Found</h1> @endif </tbody> </table> <h1 class="load_data text-center"></h1> </div> </div> @endsection
public function load_users(){ $users = User::limit(10)->get(); return view('load_users', compact('users')); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(window).scroll(function(){ var id = $('#user_id').val(); if($(window).scrollTop() + $(window).height() >= $(document).height()){ $.ajax({ url : '{{url("load_more")}}', method : "GET", data : {id:id, _token:"{{csrf_token()}}"}, dataType : "text", success : function (data) { if(data != '') { $('#remove-row').remove(); $('table tbody').append(data); } else { $('#remove-row').remove(); $('.load_data').html("No Data"); } } }); } }); </script>
Route::get('load_more', 'HomeController@load_more');
public function load_more(Request $request) { $output = ''; $id = $request->id; $users = User::where('id','>',$id)->limit(3)->get(); if(!$users->isEmpty()) { foreach($users as $user) { $output .= '<tr> <td>'.$user->id.'</td> <td>'.$user->name.'</td> <td>'.$user->email.'</td> </tr>'; } $output .= ' <tr id="remove-row"> <td></td> <td></td> <td> <img src="'.asset('images/Loading_icon.gif').'" style="width: 100px;height: 80px"> <input type="hidden" value="'.$user->id.'" id="user_id"> </td> </tr>'; echo $output; } }