jeudi 12 avril 2018

What is the best way to load page with AJAX using JSON when scroll bottom?

I have created an app like Twitter timeline using Laravel 5.6.

Web.php

Route::get('get-post', 'HomeController@view_post');

Controller

public function view_post(){
    $post = post::orderBy('created_at', 'desc')->paginate(5);
    return response()->json([
    'posts' => $post
    ]);
}

When I open http://localhost/get-post

{
   "total": 50,
   "per_page": 15,
   "current_page": 1,
   "last_page": 4,
   "first_page_url": "http://localhost/get-post?page=1",
   "last_page_url": "http://localhost/get-post?page=4",
   "next_page_url": "http://localhost/get-post?page=2",
   "prev_page_url": null,
   "path": "http://localhost/get-post",
   "from": 1,
   "to": 15,
   "data":[
        {
            // Result Object
        },
        {
            // Result Object
        }
   ]
}

I want to show this result on homepage with javascript and load more data when user put scroll to bottom.

How can I send request dynamically to http:localhost/get-post?page={1,2,3, etc..} ?

What is the best way to do this with my JSON key "next_page_url" ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire