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)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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);
}
}
Please or to participate in this conversation.