You probably need to show more of your code: You are:
if ($request->ajax()) {
yet
return view('partials.search')....
How is this ajax pagination.
I am bringing back a list of products and i have a sortby select list that users can order the products on. The problem is that when the sortby list option is changed and then i change the page on the pagination links it forgets the sortby list value and resets to the default 2nd page rather than the second page that should appear with the sortby value. For example, the default list is on highest rating and if i change to lowest rated and then click on the next page it will show the 2nd page of the default highest rated rather than the second page of the lowest rated. Does anyone know how to fix this?
Here is my code
PHP:
$productsQuery = Product::where('approved', '=', 1)->leftJoin('comments', 'comments.products_id', '=', 'products.id')->select('products.*', DB::raw('AVG(ratings) as ratings_average' ))->groupBy('products.id');
if ($request->ajax()) {
switch ($request->SortbyList) {
case 0:
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
break;
case 1:
$productsQuery = $productsQuery->orderBy('ratings_average', 'ASC');
break;
case 2:
$productsQuery = $productsQuery->orderBy('productname', 'ASC');
break;
case 3:
$productsQuery = $productsQuery->orderBy('productname', 'DESC');
break;
default:
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
}
}
else{
$productsQuery = $productsQuery->orderBy('ratings_average', 'DESC');
}
$products= $productsQuery->paginate(8);
return view('partials.search')->withProducts($products)->withSl($sl)->render();
HTML(Pagination):
{!!$products->appends(Request::all())->links('partials.pagination')!!}
Please or to participate in this conversation.