fogley's avatar

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&param2=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...

0 likes
5 replies
Glukinho's avatar

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"
]
fogley's avatar
fogley
OP
Best Answer
Level 1

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.

tykus's avatar

It should work so long as you provide all of the parameters needed by the wildcard segments as well as the query parameters

JussiMannisto's avatar

It should work. Show the actual code. There's might be a syntax issue.

jlrdw's avatar

For a query string you don't need parameters in the route.

Please or to participate in this conversation.