Reputation: 16181
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?
Upvotes: 22
Views: 34169
Reputation: 23
Get the value of the query param:
request()->query('param')
Redirect to a named route including query parameter:
redirect(route('name', ['foo' => request()->query('param')]));
Upvotes: 0
Reputation: 366
If you're building a Single Page Application (SPA) and you want to redirect to a specific page within your app from a server request, you can use the query
method from the Arr
helper class. Here is an example:
$result = Arr::query([
'result' => 'success',
'code' => '200'
]);
return redirect("/purchase?$result");
This will redirect the user to the /purchase
page with the query parameters result=success and code=200
.
For example, the final url would be:
http://example.com/purchase?result=success&code=200
Upvotes: 1
Reputation: 11636
return redirect($link);
return redirect()->route('route_name',['key'=> $value]);
Upvotes: 34
Reputation: 109
The approved answer explains it all, according to the documentation. However, if you are still interested in finding some kind of "hard-coded" alternative:
$link = "https://example.com?key1={$value1}&key2={$value2}";
Then,
return redirect($link);
If the link is to a page on your domain, you don't need to re-write the domain name, just:
$link = "?key1=${value1}&key2=${value2}";
Laravel will automatically prepend the URL with your APP_URL (.env)
Upvotes: 3