elliotk
158
5
Laravel

Sort and Paginate

Posted 7 months ago by elliotk

Hello,

I am hoping for some help and advice on sorting. I have a Category page which lists out Products which I paginate.

    public function view(Category $category)
    {   
        $category->load('media');
        $products = $category->products()->enabled()->with('media')->paginate(12);
        return view('shop.category.index', compact('category','products'));
    }

I now want to provide the option to sort the products on the category page.

Product->Name (A-z) Product->Name (Z-a) Product->Price (Low-High) Product->Price (High-Low)

What is the best way of accomplishing this? I'd considered a drop down box, wrapped by a form, which posts to the server, however I don't want any warnings about resubmitting if the user doesn't navigate properly.

I guess something like mysite.com/laptops?page=2&sort=price&price=asc would be ideal.

Any pointers how to do this in my route, controller and blade?

Thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.