Redirecting to route with additional query parameters I tried redirecting like this...
return redirect()->route('home', [
'param1' => 'val1',
'param2' => 'val2',
]);
Expecting it to redirect to the following URL...
https://domain.tld/path?param1=val1¶m2=val2
However, this is not the case. The array parameter passed to the route() function only seems to take into account variables that have been defined directly in the routes file...
Is this functionality not built in? Every piece of information I've found so far solves the problem exactly as stated above, so I'm fairly confused here...
You're doing something wrong, it should work:
// routes/web.php
Route::get('/test/{param}', [TestController::class, 'handle'])->name('testroute');
// php artisan tinker
> route('testroute', ['param' => 'value', 'other' => 'othervalue', 'another' => 'anothervalue'])
= "http://localhost:8000/test/value?other=othervalue&another=anothervalue"
> dump(redirect()->route('testroute', ['param' => 'value', 'other' => 'othervalue', 'another' => 'anothervalue']))
...
"location" => array:1 [
0 => "http://localhost:8000/test/value?other=othervalue&another=anothervalue"
]
Thanks. I knew I had done it before. I figured it out.
I used a Carbon object as value, directly. Apparently, Laravel will discard any query parameters if the value is not already a string.
Casting the Carbon object to a string solved it.
It should work so long as you provide all of the parameters needed by the wildcard segments as well as the query parameters
It should work. Show the actual code. There's might be a syntax issue.
For a query string you don't need parameters in the route.
Please sign in or create an account to participate in this conversation.