Laravel how to generate urls using GET route names?

laravel get url parameters
laravel get current url
laravel get current route
laravel get url parameters in controller
laravel route name
laravel get base url
laravel route wildcard
laravel get route name in view

I got this 2 routes in my routes file (web)

    Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController@create')->name('management/special-fees/add');
    Route::post('management/special-fees/add', 'Management\SpecialFeeController@store')->name('management/special-fees/add');

They both share the same name but one is GET and the other is POST, so far so good. But now I want to make an url in my view to open the form, for that I use the method route() like this

route('management/special-fees/add',$user->id )

but when trying to go to the url I get this route


there is a question mark instead of a "/" so the route is invalid.

I made some tests and I figured out that happens because is trying to go to the POST route instead of the GET one if I change the POST route's url in the web file like this

Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController@create')->name('management/special-fees/add');
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController@store')->name('management/special-fees/add');

I will in fact get this url


So why is the route() method generating a url for the POST route over the GET one? how do I make it to choose the GET route first?

In laravel the routing is prioritized by in the order it is written in your route.php file.

In this case you're writing the Route::post last, which in turn tells Laravel that that one should have the highest priority. Try switching them and the Route::get will have the higher priority.

Like so:

Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController@store')->name('management/special-fees/add');
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController@create')->name('management/special-fees/add');

Routing - Laravel, Basic GET Route. Route::get('/' Often, you will need to generate URLs to your routes, you may do so using the url helper: Route::get('user/{name?} Manually Specify Named Routes. This is achieved by chaining the name() method to the route definition: <?php // Define route in `routes/web.php` Route:: get ('about', function {return view ('pages.about');})-> name ('about'); // Or using a controller: Route:: get ('about', 'PageController@showAbout')-> name ('about'); Generate URLs to Named Routes. Laravel provides a global route() function that gets the URL to a named route. The first Parameter is the route name (string).

I may be wrong, but I think you'll have to re-think route naming. One of the problems route naming helps eliminate is redundant and complex names. For example, if you looked at route:list for Route::resource('something', 'SomethingController') it will have something.index, as route names for Route::get('something') and Route::post('something').

If it's the same name, it will always resolve to the first one and will probably never hit the second route; in your case will hit the POST route and never the GET route.

HTTP Routing - Laravel, In laravel the routing is prioritized by in the order it is written in your route.php file. In this case you're writing the Route::post last, which in turn  For example, imagine your application contains a route defined like the following: Route::get('/post/{post}', function () { //})->name(''); To generate a URL to this route, you may use the routehelper like so: echo route('', ['post' => 1]);//

?5 means 5 is an argument for your get route. try this


for get route insted of

route('management/special-fees/add',$user->id )

Generate URLs to Named Routes in Laravel, The $name is the name of the route to generate the URL for. Extra data can be supplied using the $parameters array parameter that will be  You may also specify route names for controller actions: Route::get('user/profile', '[email protected]')->name('profile'); {note} Route names should always be unique. Generating URLs To Named Routes. Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function:

Laravel how to generate urls using GET route names?, Does Named routes allow referring to routes when redirects or Urls more comfortably ? Route::get('user/profile', Once you have assigned a name to your routes, you may use the route's name when generating URLs or  For example, to generate a RedirectResponse to a named route, you may use the route method: return redirect()->route('login'); If your route has parameters, you may pass them as the second argument to the route method: // For a route with the following URI: profile/{id} return redirect()->route('profile', ['id' => 1]);

Laravel 5: Generating URLs to Named Routes With route, How to get current URL with parameters in Laravel 6 ? Laravel 6 - How to Get all Files in a Directory? How To Get Route Name In Controller  Today is crazy day, I want to let you know how to get route name in controller or view or middleware etc in your laravel 5.5 application. Sometime, we need to get route name for as per give on url. We can easily get route name two way using following facade: 1) Request: 2) Route:

What are named routes in Laravel and How can specify route names , Named Routes in Laravel with Laravel Tutorial, What is Laravel, Laravel given route, then you can use the name of the route while generating URLs or redirecting wraps everything, and it appears as a second parameter in a get() function. Filed Under: Laravel, Laravel 5.5, Laravel 5.6, Laravel 5.7, Laravel 5.8, Laravel 6 Tagged With: Current URL is Equal to given String, how to get current url in laravel, How to Get Route by Name, Match current url using route Name. About Yogesh Koli

  • Instead of .../management/special-fees/add?5 use .../management/special-fees/add/5