Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

R0.IT's avatar
Level 1

need to sort prodcut by price ASC and DESC

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

0 likes
13 replies
MaverickChan's avatar
Product::all()->orderBy('price','desc')->get();

asc is the same.

i would use vue.js to add a toggle button.

R0.IT's avatar
Level 1

How can I combine it in the func?

MaverickChan's avatar

you are making things complicated.

one function should only do one thing a time.

you can pass normal data to view files , then in view file , use ajax call to do sorting or anything else.

for those ajax calls , make backend functions , saprately.

R0.IT's avatar
Level 1

can u please show me how to do that?

Snapey's avatar

Why keep starting duplicate threads?

You don't need two routes

Route::get('shop/{category_url}','ShopController@products');

should work fine for what you need

Snapey's avatar

@johnef_sh if you are struggling start a new thread.

$products = Product::orderBy('name',$request->sort ?? 'ASC')->get();

if the route contains ?sort=asc or ?sort=desc then this will be used to order the query. Otherwise ASC is used

deansatch's avatar

@snapey Just out of curiosity, what would happen if someone changed the url to ?sort=hello - would it throw an error?

Snapey's avatar

@deansatch orderByuses a ternary statement to set the order

Tinker;

>>> Task::orderBy('task_name','hello')->limit(10)->get()
=> Illuminate\Database\Eloquent\Collection {#2060
     all: [
       App\Task {#2059
         id: 69,

(worked, defaults to desc)

Illuminate\Database\Query\Builder

    public function orderBy($column, $direction = 'asc')
    {
        $this->{$this->unions ? 'unionOrders' : 'orders'}[] = [
            'column' => $column,
            'direction' => strtolower($direction) == 'asc' ? 'asc' : 'desc',
        ];
        return $this;
    }

2 likes
jimamun's avatar
$all_product = Product::orderBy('created_at','desc')->get();
        return response()->json([
            'all_product'=>$all_product
        ],200);
afrasiyabhaider's avatar

@r0.it

use following code inside controller -> products funtion

Product::orderBy('price','desc')->get();

problem solved

2 likes

Please or to participate in this conversation.