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

R0.IT's avatar
Level 1

sort by the value in the url

I want to order products by price, I don't know how to get the request value and sort by the value in the url. this is the controller:

<?php


namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\Categorie;
use App\Product;
use App\Order;
use Cart, Session ,DB;


class SortController extends MainController{
    
 public function sortBy(Request $request)
{
    $sort = $request->get('sort', null);

    $products = Product::when($sort, function ($Squery) use ($sort) {

        $Squery->orderBy('price', 'desc');
    })->get();
    
  return view('content.sort')->with('product', $products);

}
}




0 likes
5 replies
topvillas's avatar

If your route is something like this ...

Route::get('/sort/{field}', 'SortController@sortBy')

Then the "field" parameter will be passed as the first argument to the method.

public function sortBy($field, Request $request)
R0.IT's avatar
Level 1

this is my route:

Route::get('shop/{category_url}/sort=ASC','SortController@sortBy');

how can I combine it that in the end the products will be orderd by price?

topvillas's avatar

Change your route to something like this and then read the docs about routes.

Route::get('shop/{category_url}/sort/{field}/{direction}', 'SortController@sortBy');
public function sortBy($category_url, $field, $direction, Request $request)
maddoxstone's avatar

Guys, can you help me with casino land website, I am not sure that I code everything right, maybe any of you can check?

Please or to participate in this conversation.