Product::all()->orderBy('price','desc')->get();
asc is the same.
i would use vue.js to add a toggle button.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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','ShopController@products');
Route::get('shop/{category_url}?sort=ASC','ShopController@products');
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 or to participate in this conversation.