Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

baloch's avatar

laravel redirect back to post route, i get MethodNotAllowedHttpException

At first on a get request i show a form, on submitting it, it goes to a post url does some working in the controller and then returns another view which has a form. after submitting this form i validate some things and if the validation fails i want to redirect back with some errors but unfortunately laravel isn't allowing me to redirect back to a post method. Is there any way i can accomplish this?

i have seen this answer on laracast https://laracasts.com/discuss/channels/laravel/redirect-back-to-post-route-i-get-methodnotallowed-exception but i couldn't grasp the recommendation

these are my routes in the order described above.

Route::get('/purchasePacks', 'masterDealerController@purchasePacks')->name('purchasePacks');
  Route::post('/confirmPuchasedPacks','masterDealerController@confirmPuchasedPacks');
  Route::post('/storeOrder','masterDealerController@storeOrder');

below is the redirection code in the storeOrder method. which redirects back to the post url

Alert::message("Unable to complete your payment at the moment.",'Please try again later.',"error");
        return back()->withInput();
0 likes
9 replies
AlexDemin's avatar

Please list your routes and the controller method with the redirect statement.

AlexDemin's avatar

Why not add an intermediate get route to show the second form?

Route::get('/purchasePacks', 'masterDealerController@purchasePacks');
Route::post('/confirmPuchasedPacks','masterDealerController@confirmPuchasedPacks');
Route::get('/confirmPuchasedPacks','masterDealerController@showPuchasedPacks');
Route::post('/storeOrder','masterDealerController@storeOrder');
baloch's avatar

@AlexDemin because when the request comes to this url Route::post('/confirmPuchasedPacks','masterDealerController@confirmPuchasedPacks'); then in the confirmPuchasedPacks method i process the request and make some variables. Then I return a view and pass those variables to that view. so basically the view requires those variables.

AlexDemin's avatar

@baloch no problem, you can store variables in DB, session or cache.

In showPuchasedPacks method, you will be able to load these variables and transmit them to the desired view.

Snapey's avatar
Snapey
Best Answer
Level 122

simple rule to make your life easier... NEVER return a view in response to a POST request. Always redirect somewhere else which shows the result of the post or displays the next form.

5 likes
baloch's avatar

@Snapey this is a good idea but what if i have to use those post values in the view?

Snapey's avatar

Either flash the values to the session or store them in the database and then retrieve them on the next view.

ioanandrei's avatar

Do you mean, the method that is linked to the POST route should return redirect(url()) to a GET url that is "waiting" the information from the previous method, right?

Please or to participate in this conversation.