0

I access a page of Route::post('search'). From this page, I perform a form submission using a POST method. If the validation fails, I get error The GET method is not supported for this route. Supported methods: POST. probably because when Laravel validation redirects back to the same page, it redirects as a GET method.

I am unable to change nor add a new Route for search due to strict rules here. How should I redirect back to search page as POST method?

This is from CustomFormRequest.php

protected function failedValidation(Validator $validator)
{
  \Log::debug("WAS HERE");
  \Log::debug(print_r($this->request->all(), true));
  \Log::debug(print_r($this->request->method(), true));
}

I can see the request but not the method. Meaning I cant update the method manually to try and check if this would work. Though if I check the value in controller without the formrequest, I can see both.

I tried searching for SO questions but nothing really about my concern.

How should I redirect to the same route using POST method?

7
  • Why the search method using POST though? I have yet to see a good reason for this excluding the fact that, it is just a big inconvenience for a user. Commented Jul 13, 2023 at 2:12
  • The previous programmer used a form tag to perform search function. That is why the route for the search is POST method. Commented Jul 13, 2023 at 2:14
  • The GET method is not supported for this route. Supported methods: POST. has nothing to do with the validation layer, that error is on the routing layer. You have declared a post route, but are making a get request to that route. Commented Jul 13, 2023 at 7:34
  • probably because when Laravel validation redirects back to the same page, it redirects as a GET method. so you are using a POST request to load the page? Commented Jul 13, 2023 at 7:42
  • so you are using a **POST** request to load the page Yes I am accessing the search page using a post method. from here, perform a post form submission Commented Jul 13, 2023 at 8:38

1 Answer 1

0

Several ways to get around this:

  1. Add another get route to the same controller action.
  2. Allow get and post on the same route:
Route::match(['get', 'post'], '/', function () {
    // ...
});
  1. Use get instead of post, even if a form is used:
<form action="/search" method="get">
    <input type="text" name="q"/>
    <input type="submit" value="Search"/>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Like my question above, I am unable to change the method to get since it will append the query to the URL, which the client does not want. That is why I am stuck on using POST route as is. If I add match get/post, when it redirects, the previously searched information will be cleared since again the searched values are not in the URL.
Normally, this would be the correct answer but not in my case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.