I have a route
Route::get('/edit/{id}', 'EditController')->name('edit');
I have a route param named id, but I also need query param id, example:
/edit/1?id=9999
When I use
route('edit', ['id' => 1])
Only route param is passed
/edit/1
Since query param name can not change, I would like to ask if there is any other way other than
- Change the route param
Route::get('/edit/{someOtherName}', 'EditController')->name('edit');
- Concat query string
route('edit', ['id' => 1]) . "?" . http_build_query(['id' => 9999])
2
Answers
Yes, you would need to either change the route key name or manually append the query string to the generated url.
The
route
helper’s signature isIt does not have a way to specify query parameters and route parameters separately. The
$parameters
array accounts for both.Eventually, the
route()
helper will call theIlluminateRoutingRouteUrlGenerator
class’sto
method which has a very similar signatureThere, the route keys are replaced from the
$parameters
array and what is left over gets added to the query string.Since PHP does not support having duplicate keys in it’s associative arrays, you are stuck with the two solutions you came up with.
You should try this
And in controller you can get id parameter by using this
It will return 9999, and for getting 1 you can simply pass it as like this