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

Danlog's avatar

Validation problem: The POST method is not supported for route /

Hello! I’m having quite a weird situation here:

$validated = $request->validate([
            'name' => ['required', 'min:1', 'max:255'],
            'products_quantity' => ['required', 'numeric', 'max:7'],
            'product_id' => ['required', 'numeric', new ExistsInDatabase],
        ]);

I’m getting the following error:

The POST method is not supported for route /. Supported methods: GET, HEAD. 

But whenever I delete numeric from both and new ExistsInDatabase it works just fine. I’m trying to work this out as an api function. I’m quite confused at this error because the endpoint is indeed POST a nd I’m not getting any information about the problem with the validation…

Here is ExistsInDatabase:

public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if(!Product::where('id', $value)->exists()){
            $fail('product not found');
        }
    }

Here is how the api enpoint looks like:

Route::post('/create/products', [\App\Http\Controllers\AdminController::class, 'createBatch']);

Why is this failing?

1 like
15 replies
LaryAI's avatar
Level 58

The error message you're seeing, The POST method is not supported for route /. Supported methods: GET, HEAD, suggests that the form or API request you're making is being sent to the wrong URL (/), rather than the intended /create/products endpoint.

Here are a few things to check and try:

  1. Form Action URL: If you're submitting a form, ensure that the action attribute of your form tag is set to the correct URL (/create/products).

  2. API Request URL: If you're making an API request from a client like Postman or using AJAX, ensure that the URL is correctly set to /create/products.

  3. CSRF Token: If you're using Laravel and making a POST request from a form, ensure that you have included a CSRF token in your form.

  4. Route Caching: If you've recently added or changed routes, you might have a cached version of your routes that doesn't include the new changes. Run php artisan route:clear to clear the route cache.

  5. Middleware: Ensure that there's no middleware that might be redirecting your POST request to /.

  6. Validation Rules: The max:7 rule for products_quantity should be max:9999999 if you want to allow up to 7 digits, as max in numeric validation refers to the value, not the number of characters.

Here's a revised version of your validation rules:

$validated = $request->validate([
    'name' => ['required', 'min:1', 'max:255'],
    'products_quantity' => ['required', 'numeric', 'max:9999999'],
    'product_id' => ['required', 'numeric', 'exists:products,id'],
]);

Note that I've replaced new ExistsInDatabase with 'exists:products,id', which is the standard way to check for the existence of a record in Laravel validation.

If none of these suggestions resolve the issue, you may want to check your server logs or Laravel logs for more detailed error messages that could provide additional insight into the problem.

Danlog's avatar

@LaryAI Hello AI. Unfortunately, I'm still getting the The POST method is not supported for route /. Supported methods: GET, HEAD. error.

shariff's avatar

@Danlog Can you show your blade and route file ?. May be you are not passing method as post in <form> tag.

<form method="post">
   @csrf
</form>

Danlog's avatar

@shariff Hey! I'm using this for an API, I'm using insomnia (the api development platform). The request is POST, and I'm submitting the fields (I double checked for any typos) correctly. It's extremely confusing to me what's happening, to be honest.

I included the endpoint in the question which is located inside the api.php file.

I think the problem has to do with the ExistsInDatabase Rule

shariff's avatar

@Danlog I feel this is not related to validation. It is not hitting a proper endpoint. You can try removing the validation and check whether you are getting a proper response or not. Sometimes it will happen because of cache issue. Try clearing cache php artisan config:cache and php artisan route:clear.

Danlog's avatar

@shariff Yes, I cleared the cache. The request seems to go through when I remove the new ExistsInDatabase rule from the validation

shariff's avatar

@Danlog Can you tell what is the use of this valiation? You are checking whether the product_id already exist or not?

'product_id' => ['required', 'numeric', 'exists:products,id'],
Danlog's avatar

@shariff I'm using product_id to generate lots of copies of that product (I know sounds confusing), but see it as: you select a present, and you generate products based on that preset. Each product is going to be subject to modifications by the user, for example.

shariff's avatar

@Danlog Then that logic you can try writing outside validation Instead of Request class

Snapey's avatar

easy enough to check in the controller if it is being reached, but clearly it is as changing validation behaviour alters the outcome

perhaps failing one of the other validations would convince you that the problem is actually the redirection AFTER validation fails

And to get a json response and not an HTTP redirect you need to send that you accept json response in the header- are you doing this?

1 like
ezz-eldin's avatar

Hello How you doing

as laravel validate in web it return to redirect route which give get request will submitting post request

protected function failedValidation(Validator $validator) { $exception = $validator->getException();

    throw (new $exception($validator))
                ->errorBag($this->errorBag)
                ->redirectTo($this->getRedirectUrl());
}

you can modify code in FormRequest to

protected function failedValidation(Validator $validator) { $exception = $validator->getException();

    throw new HttpResponseException(response()->json($validator->errors(), 422));

    // throw (new $exception($validator))
    //             ->errorBag($this->errorBag)
    //             ->redirectTo($this->getRedirectUrl());
}
Snapey's avatar

@ezz-eldin no need to mess with anything if you make sure your request includes the accept header to tell laravel you need a json reply

nabilkrs's avatar

Hello I'm facing the same problem any solution please?

nabilkrs's avatar

Problem solved! I just added accept application/json in the header

1 like

Please or to participate in this conversation.