dionarap's avatar

Pagination and sortby

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')!!}
0 likes
7 replies
jlrdw's avatar

You probably need to show more of your code: You are:

if ($request->ajax()) {

yet

return view('partials.search')....

How is this ajax pagination.

dionarap's avatar

@jlrdw the JS works... its just getting the wrong page and i'm pretty sure thats due to the php above

jlrdw's avatar

The case has to be in the query string and you going to need links on The View page.

In the link you have to pass the updated or new case and of course re load the page.

I would imagine if changing case you are probably going to start all over again with the pagination.

To get what you want I think you're going to need something like data tables where you can change the sort on the Fly.

pranta_bhuiyan's avatar

you can try:

// php

 return view('partials.search',compact($products));
dionarap's avatar

@PRANTA_BHUIYAN - That won't make much of a difference. The issue is that the sortby seems to be forgotten when the pagination(next page) is clicked and it just returns the default second page

jlrdw's avatar

Have you tested paginating with a group by without

    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');


To see if the paginator works:

From the docs:

Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

So see if it is working first, no ajax.

Just use one of the order by's and see if it works.

dionarap's avatar

@JLRDW - Cheers for the help man, misread what you meant by the first post. That post is much clearer. Seems the error is with my JS after all

Please or to participate in this conversation.