Skip to content
Advertisement

Laravel 7 API pagination is not working after first time

I am trying search functionality through API. I am getting the response with pagination details for the first time but to load more results next_page_url is giving no results

GET http://127.0.0.1:8000/api/search?keyword=cityname&minPrice=0&maxPrice=0&bed=any&propertyType=RESEDENTIAL

I am retuning the results after some very basic filters

$results = $searchQuery->paginate(10);

    return response()->json(['results' => $results], 200);

Response I am getting is

 "first_page_url": "http://127.0.0.1:8000/api/search?page=1",
 "from": 1,
 "last_page": 96,
 "last_page_url": "http://127.0.0.1:8000/api/search?page=96",
 "next_page_url": "http://127.0.0.1:8000/api/search?page=2",
 "path": "http://127.0.0.1:8000/api/search",
 "per_page": 10,
 "prev_page_url": null,
 "to": 10,
 "total": 956

To load more results I am sending a get request to ‘next_page_url’

GET http://127.0.0.1:8000/api/search?page=2

response is

"results": {
    "current_page": 2,
    "data": [],
    "first_page_url": "http://127.0.0.1:8000/api/search?page=1",
    "from": null,
    "last_page": 1,
    "last_page_url": "http://127.0.0.1:8000/api/search?page=1",
    "next_page_url": null,
    "path": "http://127.0.0.1:8000/api/search",
    "per_page": 10,
    "prev_page_url": "http://127.0.0.1:8000/api/search?page=1",
    "to": null,
    "total": 0
}

My API Route: Route::get('/search', 'SearchController@index');

Advertisement

Answer

Finally I am able to solve by doing simple suggestion by a friend.

I use POST method to search with query params as form data

POST http://127.0.0.1:8000/api/search

then I use next page url as same as above (query params as form data)

POST http://127.0.0.1:8000/api/search?page=2

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement