Redirect to current URL while changing a query parameter in Laravel
Asked Answered
L

7

6

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.

Limitless answered 8/2, 2014 at 18:32 Comment(0)
S
8

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()

Swann answered 8/2, 2014 at 18:53 Comment(3)
Does it append them to query string only when using a named route? URL::to() adds them to the URL as segments. See my edit.Limitless
@MarttiLaine - It doesn't seem to using ::to(). I'm not really sure that, structurally, I'd recommend that. Is there some reason why you can't use URL::action('MyController@search')?Swann
@MarttiLaine using Url::route() as I suggested will add the second parameter to the query stringScuffle
S
7

I prefer native PHP array merging to override some parameters:

['type' => 'link'] + \Request::all()

To add or override the type parameter and remove another the term:

['type' => 'link'] + \Request::except('term')

Usage when generating routes:

route('movie::category.show', ['type' => 'link'] + \Request::all())
Shrum answered 24/12, 2016 at 8:48 Comment(0)
S
2

You can do it with Laravel's URLGenerator

URL::route('search', array(
  'term' => Input::get('term'),
  'link' => Input::get('type')
));

Edit: be sure to name the route in your routes.php file:

Route::get('search', array('as' => 'search'));

That will work even if you're using a Route::controller()

Scuffle answered 8/2, 2014 at 18:50 Comment(0)
E
1

From Laravel documentation:

if your route has parameters, you may pass them as the second argument to the route method.

In this case, for return an URI like example.com/search?term=foo&type=user, you can use redirect function like this:

return redirect()->route('search', ['term' => 'foo', 'type' => 'user']);
Enciso answered 23/2, 2018 at 15:7 Comment(0)
M
0

Yes, there is a built in way. You can do your manipulation in Middleware.

The $request passed to the handle method of all middleware has a query property. As an InputBag, it comes with a few methods; Namely, for your intentions: ->set().

Pretty self explanatory, but here's an example:

public function handle(Request $request, Closure $next)
{
    $request->query->set('term','new-value');

    // now you pass the request (with the manipulated query) down the pipeline.
    return $next($request);
}
Murder answered 23/11, 2020 at 16:1 Comment(0)
B
0

Necroposting. But "request" helper usable since Laravel 5 if i'm not mistaken

request()->fullUrlWithQuery(['orderBy'=>'count', 'dir'=>'asc'])
Brame answered 9/10, 2024 at 11:41 Comment(0)
M
-3

The Input component should also contain query parameters.

i.e Input::get('foo');

Matthiew answered 8/2, 2014 at 18:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.