Laravel Load More Data on Page Scroll – Laravel 5

Submitted by dave - 7 years ago

Laravel Load More Data on Page Scroll – Laravel 5

// see more at :: http://www.onlinecode.org/laravel-load-more-data-on-page-scroll-laravel-5/

namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
use App\Post;
 
use Helper;
// LoadController Controller for Laravel Load More Data
class LoadController extends Controller
{
    // Dataload function
    public function Dataload()
    {
		// get post data in DESC order
        $post_data = Post::orderBy('created_at','DESC')->limit(2)->get();
        // return DESC order data
        return view('demos.loaddata')->withPosts($post_data);
    }
    
	// function DataAjaxload
    public function DataAjaxload(Request $request)
    {
        $set_output = '';
        $id = $request->id;
        
        $post_data = Post::where('id','<',$id)->orderBy('created_at','DESC')->limit(2)->get();
        
        if(!$post_data->isEmpty())
        {
            foreach($post_data as $post_val)
            {
                $set_url = url('blog/'.$post_val->slug);
                $body_data = substr(strip_tags($post_val->body),0,500);
                $body_data .= strlen(strip_tags($post_val->body))>500?"...":"";
                                
                $set_output .= '<div class="mdl-grid grids mdl-cell mdl-shadow--4dp col-md-12  ">
                                <div class="post grids">
                                    <a href="'.$set_url.'" class="nounderline grids" ><h2 class="grids post-title" >'.$post_val->title.'</h2></a>
                                    <div class="row grids">
                                       <div class="col-md-6 grids">
                                           <h5 class="post-date grids" >Published:'.date('M j, Y', strtotime($post_val->created_at)).'</h5> 
                                       </div>
                                   </div>
                                    <div class="row">
                                        <div class="col-md-4 post-img1 grids">
                                            <img  class="img-responve1 grids"  src="'.Helper::catch_first_image($post_val->body).'" alt="'.$post_val->title.'" > 
                                        </div>
                                        <div class="col-md-8 grids">
                                            <p class="text-justify">'.$body_data.'</p>
                                        </div>
                                    </div>
                                </div>   
                            </div>';
            }
            
            $set_output .= '<div id="remove-row grids">
                            <button id="btn-more " data-id="'.$post_val->id.'" class="nounderline grids btn-block mdl-button mdl-js-button" > Load More </button>
                        </div>';
            
            echo $set_output;
        }
    }
}