jeudi 4 juillet 2019

How to create sort links using pagination?

I am working on simple online store. I'm displaying paginated list of items in stock and I would like to make links that user can click in order to change sort order of item (e.g. sort by price, name).

My index function in ItemsController (one page has only one item, because I want to see links to other pages):

    function index()
    {
        // selecting items that are available
        $items = Item::whereHas('amount', function($query)
        {
            $query->where('amount','>',0);
        })->paginate(1);
        // selecting main_categories for main menu
        $main_categories = Category::where('parent_id', null)->get();


        return view('home', compact('items', 'main_categories'));
    }

In my 'home' view:

    <div id="filter-menu" class="col-12 text-center">
        
    </div>

    @include('partials.product_list')


    

$items->appends(['sort' => 'price'])->render() is just creating another buttons with pages to choose from but after clicking items are sorted using price column (/?sort=price&page=2 is appended to the link). I would like to make links that will change sorting order (something like sort links in CakePHP's paginator: https://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-sort-links). Is there anything in Laravel that could help me achieve expected result?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire