Of course that's possible
You can build up your eloquent call with the parameters from the request, so you get something like
return User::orderBy('email','desc')->paginate(20);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I have in my code:
public function index()
{
return User::paginate(20);
}
I kinda remember something likewise from L4, which also accepted sort= as parameter in the request, but that's not working?
Now I can do: /users?page=10 to get page 10, but I also wanna sort my things with a URL like /users?page=10&sort=email&direction=desc
Isn't that possible anymore with L5?
If you're going down that route, you could make a little bit more dynamic.
if($request->has('sort') {
return User::orderBy(
$request->get('sort'),
$request->get('direction') ?: 'desc'
)->paginate(20);
}
return User::paginate(20);
Edit: Although I'm not sure you can check for the direction value in my code above. So this might work (untested).
if($request->has('sort')
{
$direction = $request->get('direction') ?: 'desc';
return User::orderBy($request->get('sort'), $direction)->paginate(20);
}
return User::paginate(20);
Please or to participate in this conversation.