0
votes

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

.../management/special-fees/add?5

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

.../management/special-fees/addSSSS?5

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?

3
Instead of .../management/special-fees/add?5 use .../management/special-fees/add/5Vijay Sankhat

3 Answers

0
votes

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');
0
votes

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, something.store 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.

0
votes

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

url('management/special-fees/add/'.$user->id)

for get route insted of

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