Auto Load More Data on Page Scroll with jQuery and PHP Laravel
In this post we will give you information about Auto Load More Data on Page Scroll with jQuery and PHP Laravel. Hear we will give you detail about Auto Load More Data on Page Scroll with jQuery and PHP LaravelAnd how to use it also give you demo for it if it is necessary.
In this tutorial, I will tell you how to auto load more data on page scroll from database using jQuery and PHP Laravel.
You don’t have need to click anywhere to load data because data is loading on page scroll automatically from MySQl database.
There are so many website that use the same logic to load data on page scroll, this is very useful with several cases.
It will check if data length is equal to 0 then display message for “No more records!” and if data length is more than 0 then it append html data to list.
Here is a simple step to let you know about loading data automatically on page scroll from top to bottom using jQuery and PHP.
Step1: Create Table and Model
In this step, we will create first products table and model.
app/Product.php
- <?php
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Product extends Model
- {
- public $fillable=['name','details'];
- }
<?php namespace App; use IlluminateDatabaseEloquentModel; class Product extends Model { public $fillable = ['name','details']; }
I have inserted some dummy records in my products table to see the loader on page scroll.
Step2: Add Routes
In this step, we will add routes to handle request.
app/Http/routes.php
- Route::get('jquery-loadmore',['as'=>'jquery-loadmore','uses'=>'FileController@loadMore']);
Route::get('jquery-loadmore',['as'=>'jquery-loadmore','uses'=>'FileController@loadMore']);
Step3: Create FileController
In this step, we will create a file controller in following path app/Http/Controllers.
app/Http/Controllers/FileController.php
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppHttpControllersController;
- use AppProduct;
- class FileController extends Controller {
- public functionloadMore(Request $request){
- $products=Product::paginate(4);
- $html='';
- foreach($productsas$product){
- $html.='<li>'.$product->id.' <strong>'.$product->name.'</strong> : '.$product->details.'</li>';
- }
- if($request->ajax()){
- return$html;
- }
- returnview('files.loadmore',compact('products'));
- }
- }
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpControllersController; use AppProduct; class FileController extends Controller { public function loadMore(Request $request){ $products=Product::paginate(4); $html=''; foreach ($products as $product) { $html.='<li>'.$product->id.' <strong>'.$product->name.'</strong> : '.$product->details.'</li>'; } if ($request->ajax()) { return $html; } return view('files.loadmore',compact('products')); } }
Step4: Create Blade File
In this step, we will create a view file within files directory so first create files directory and then create loadmore.blade.php
within file directory directory (resources/views/files/loadmore.blade.php)
resources/views/files/loadmore.blade.php
- @extends('layouts.default')
- @section('content')
- <style>
- .wrapper > ul#results li {
- margin-bottom: 1px;
- background: #f9f9f9;
- padding: 20px;
- list-style: none;
- }
- .ajax-loading{
- text-align: center;
- }
- </style>
- <div class="wrapper">
- <ul id="results"><!-- results appear here --></ul>
- <div class="ajax-loading"><img src="{{ asset('images/loading.gif') }}"/></div>
- </div>
- <script>
- var page =1;//track user scroll as page number, right now page number is 1
- load_more(page);//initial content load
- $(window).scroll(function(){//detect page scroll
- if($(window).scrollTop()+ $(window).height()>= $(document).height()){//if user scrolled from top to bottom of the page
- page++;//page number increment
- load_more(page);//load content
- }
- });
- functionload_more(page){
- $.ajax(
- {
- url:'?page='+ page,
- type:"get",
- datatype:"html",
- beforeSend:function()
- {
- $('.ajax-loading').show();
- }
- })
- .done(function(data)
- {
- if(data.length==){
- console.log(data.length);
- //notify user if nothing to load
- $('.ajax-loading').html("No more records!");
- return;
- }
- $('.ajax-loading').hide();//hide loading animation once data is received
- $("#results").append(data);//append data into #results element
- })
- .fail(function(jqXHR, ajaxOptions, thrownError)
- {
- alert('No response from server');
- });
- }
- </script>
- @endsection
@extends('layouts.default') @section('content') <style> .wrapper > ul#results li { margin-bottom: 1px; background: #f9f9f9; padding: 20px; list-style: none; } .ajax-loading{ text-align: center; } </style> <div > <ul id="results"><!-- results appear here --></ul> <div ><img src="{{ asset('images/loading.gif') }}" /></div> </div> <script> var page = 1; //track user scroll as page number, right now page number is 1 load_more(page); //initial content load $(window).scroll(function() { //detect page scroll if($(window).scrollTop() + $(window).height() >= $(document).height()) { //if user scrolled from top to bottom of the page page++; //page number increment load_more(page); //load content } }); function load_more(page){ $.ajax( { url: '?page=' + page, type: "get", datatype: "html", beforeSend: function() { $('.ajax-loading').show(); } }) .done(function(data) { if(data.length == 0){ console.log(data.length); //notify user if nothing to load $('.ajax-loading').html("No more records!"); return; } $('.ajax-loading').hide(); //hide loading animation once data is received $("#results").append(data); //append data into #results element }) .fail(function(jqXHR, ajaxOptions, thrownError) { alert('No response from server'); }); } </script> @endsection
Click here to see demo..
Hope this code and post will helped you for implement Auto Load More Data on Page Scroll with jQuery and PHP 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