Lance
Lance

Reputation: 3213

return back()->withInput($request->input()) throws The GET method is not supported for this route. Supported methods: POST

I am getting this error "The GET method is not supported for this route. Supported methods: POST." if there are any errors in a form.

I can successfully post to my form but it throws during the return process.

web.php

Route::post('/services/book/hotel', 'HotelController@showbook');
Route::post('/services/book/hotelroom', 'HotelController@book');

book function is fairly lengthy but ends in

    if($responseSuccess){
        $error = [];
        $bookingDetail = json_decode($response['BookingDetail']);

        return Redirect::to(url('my-account'));
    } else {
        $error = $response['message'];
        $hotel = [];

        session()->flash('alert-class', 'alert-danger'); 
        session()->flash('message', $error); 

    // dd(__method__.'::'.__line__,get_defined_vars(),$request->input());
        return back()->withInput($request->input());
    }

if I uncomment the dd I get see that everything looks like I would expect as far as the errors and the inputs but when I hit the return I see. What am I doing wrong?

I have read throught dozens of articles here on slack and other sites that talk about the "The GET method is not supported for this route. Supported methods: POST." but none of them seem to have the issue when "returning back"

enter image description here

I am using PHP 7.2 and Laravel/framework 6.2

enter image description here

If I try to change the form from a post to a get I recieve this error because there is a lot of data being passed. And the client prefers this in a post so it can't be bookmarked or emailed.

enter image description here

This page is passing a lot of "hidden" variables and I want to be able to display error if needed and still have the variables. Is that not possible?

enter image description here

Upvotes: 1

Views: 601

Answers (2)

nowendwell
nowendwell

Reputation: 11

The back() helper is a redirect to a page using the GET method. If it is trying to GET /services/book/hotel or /services/book/hotelroom it will fail.

What is the url of the page that shows the form?

Upvotes: 1

Razor
Razor

Reputation: 9855

You can use the HTTP 307 Temporary Redirect:

return back(307)->withInput();

Better solution:

Instead of changing the <form> action attribute from POST to GET, you need to change the page that shows the form:

Route::get('/services/book/hotel', 'HotelController@showbook');
Route::post('/services/book/hotelroom', 'HotelController@book');

Upvotes: 1

Related Questions