Redirect to current URL while changing a query parameter in Laravel
laravel, laravel-4
Solution
You can use the URL Generator to accomplish this. Assuming that search is a named route:
$queryToAdd = array('type' => 'user');
$currentQuery = Input::query();
// Merge our new query parameters into the current query string
$query = array_merge($queryToAdd, $currentQuery);
// Redirect to our route with the new query string
return Redirect::route('search', $query);
Laravel will take the positional parameters out of the passed array (which doesn't seem to apply to this scenario), and append the rest as a query string to the generated URL.
See: `URLGenerator::route()`, `URLGenerator::replaceRouteParameters()` `URLGenerator::getRouteQueryString()`
Problem
Is there a built-in way to do something like this? Let's say I have a search-page that has a few parameters in the URL: ``` example.com/search?term=foo&type=user ``` A link on that page would redirect to an URL where `type` is `link`. I'm looking for a method to do this without manually constructing the URL. Edit: I could build the URL manually like so: ``` $qs = http_build_query(array( 'term' => Input::get('term'), 'type' => Input::get('type') )); $url = URL::to('search?'.$qs); ``` However, what I wanted to know is if there is a nicer, built-in way of doing this in Laravel, because the code gets messier when I want to change one of those values. Giving the URL generator a second argument (`$parameters`) adds them to the URL as segments, not in the query string.