Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect to a URL with query parameters?

I am trying to do a redirect with query parameters, using the redirect() helper:

$link = 'https://example.com' . '?key1=value1&key2=value2';

return redirect()->to($link);

The problem is that when the $link is passed to the to() method Laravel removes the question mark leading the query string, so it turns this:

https://example.com?key1=value1&key2=value2

into this:

https://example.comkey1=value1&key2=value2

(again, notice the missing ? in the final link).

How do I make a redirect with query params appended to a custom URL?

like image 866
lesssugar Avatar asked Jan 31 '18 12:01

lesssugar


People also ask

Can you redirect URLs with parameters?

Redirect with parameters to a specific page/folder/subfolder You can also configure a URL redirect record that includes folders/subfolders both of your source and destination URLs.

How do I pass parameters to URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How do I automatically redirect a URL?

To redirect from an HTML page, use the META Tag. With this, use the http-equiv attribute to provide an HTTP header for the value of the content attribute. The value of the content is the number of seconds; you want the page to redirect after.


1 Answers

Use:

return redirect($link);

If you want to redirect a named route with query string, use:

return redirect()->route('route_name',['key'=> $value]);

Documentation

like image 58
Sapnesh Naik Avatar answered Oct 23 '22 07:10

Sapnesh Naik