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

mardev's avatar

Sort query result by URL

Hey guys!

I’m working on a project which is a search engine for TV series. On the first page you can choose genres, release year, actors, etc. The site goes through the db and lists out all series matching the users criteria. I now want the users to be able to sort through the result by ‘A-Z’, ‘Oldest’ and ‘Newest’. So far by adding a hidden input in the form I’m able to order the result by title, and if I manually change the sort value in the url it works, but how can I make it happen automatically when for instances a user presses a link or a button?

This is the start of my form: {!! Form::open(array('route' => 'result', 'method' => 'GET')) !!} {!! Form::hidden('sort', 'title') !!}

This where I get the sort variable from the URL: OrderBy((Input::get('sort')), 'asc')->paginate(10);

And this is what my URL looks like (not the prettiest I know...): http://localhost:8888/result?sort=title&title=&action=1&adventure=2&drama=7&actors=

Thanks!

0 likes
3 replies
bobbybouwmann's avatar
Level 88

You can just pass in the variables like a normal route. However make sure your route does not accept any parameters. If your does not accept any parameters Laravel will add them like a GET request.

<a href="{{ route('myroute', ['sort' => 'title', 'title' => 'asc']) }}">Sort by title</a>
mardev's avatar

Thanks for replying @bobbybouwmann ! How do I get the parameters into my controller? And can I do the same to a route in Form::open?

bobbybouwmann's avatar

You can simply do this in your controller

$sort = Request::get('sort');
$title = Request::get('title');
$allParameters = Request::all():

You can do do the same thing in Form::open

Please or to participate in this conversation.