R0.IT
566
9
Laravel

need to sort prodcut by price ASC and DESC

Posted 1 year ago by R0.IT

I need to sort prodcut by price ASC and DESC I have created the controller:

   public function products($category_url, Request $request){
        Product::getProducts($category_url, self:: $data);
        if ($category1 = Categorie::where('url', '=', $category_url)->first()) {
           $sort = $request->get('sort', 'asc');
           $products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
          $sort = $sort == 'asc' ? 'desc' : 'asc'; 
           return view('content.products', self::$data , compact('products', 'sort'));
       

the route

Route::get('shop/{category_url}?sort=DESC','[email protected]');
Route::get('shop/{category_url}?sort=ASC','[email protected]');

the link from the view

  <a href="{{ url('shop/'.$category['url'])}}?sort=DESC" style="color:black"> High to low</a> |
    <a href="{{ url('shop/'.$category['url'])}}?sort=ASC" style="color:black">Low to high</a>

But when I click on them the order doesn't change, any can please advice? Thanks

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

Reply to

Use Markdown with GitHub-flavored code blocks.