首页 > 解决方案 > 通过 axio 请求的分页问题

问题描述

我正在尝试向 laravel 后端发送 axio fetch 请求

url = /search?searchBy=product_id&searchValue=sprint-10

Route::get('/search', 'ProductController@search');

 public function search(Request $request){
 $requestQuery = $request->query(); //to get all the queries

     $searchBy = $request->searchBy;
     $searchValue = $request->searchValue;

     if( $searchBy == 'product_id' && $searchValue == true ) {
         $product = Product::where("product_id", 'like', "%$searchValue%")->paginate(5);
         return response($product);
     } else {
         return $requestQuery;
     }

    }

这将返回带有分页选项的产品......当我点击 next_page_url 时,它会转到

next_page_url: " http://sprint/search?page=2 ",

我没有从第 2 页得到结果..

你能告诉我如何解决这个问题....

标签: laravelpagination

解决方案


我找到了答案...

我们必须使用 appends($request->except('page'));

$product = Product::where("product_id", 'like', "%$searchValue%")->paginate(5);
return $product->appends($request->except('page'));

结果 :

next_page_url: "http://sprint-laravel/search?searchBy=product_id&searchValue=sprint-10&page=2",

推荐阅读