How to pass Query parameter using route name in laravel?
Asked Answered
C

3

7

I know this one

{{route('editApplication', ['id' => $application->id])}} == /application/edit/{id}

But I need

?? == /application/edit?appId=id

Anyone, please replace the "??" with your answer that helps me out.

Cheryllches answered 8/1, 2020 at 6:21 Comment(1)
Can you please elaborate more about what you want to achieve, where you want to achieve?Meshuga
E
26

It depends how you route looks like:

If you route is:

Route::get('/application/edit/{id}', 'SomeController')->name('editApplication');

when you use

route('editApplication', ['id' => 5])

url will be like this:

/application/edit/5

However all other parameters (that are not in route parameters) will be used as query string so for example:

route('editApplication', ['id' => 5, 'first' => 'foo', 'second' => 'bar'])

will generate url like this:

/application/edit/5?first=foo&second=bar

In case you want to achieve url like this

/application/edit?appId=id

you should define route like this:

Route::get('/application/edit/', 'SomeController')->name('editApplication');

and then when you use

route('editApplication', ['appId' => 5])

you will get

/application/edit?appId=5

url.

Estuary answered 8/1, 2020 at 6:32 Comment(1)
HIGHLY IMPRESSIVE AND CORRECT ANSWER, Thanks Marcin Nabialek :)Stuffing
R
2

You need to modift your "editApplication" route without parameter option. If you stil want the parameter just add the following row in your controller and just catch the data by using;

$appId = $request->input('appId');
Robins answered 8/1, 2020 at 7:19 Comment(0)
P
1

route method accepts any set of variable and puts them as query string unless the variable name matches with defined route signature.

Thus, for your case you can do something as follows:

route('editApplication', ['appId' => <your id here>])

You can provide any number of variables with the array.

Puberulent answered 8/1, 2020 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.