Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel returns 405 Method Not Allowed for a form or API call

Error405 Method Not Allowed

Short answer

A 405 means the URL exists but not for the HTTP verb you sent. In Laravel that is a verb mismatch between the request and the route: a form sending POST to a PUT route, a call using GET on a POST route, or a catch-all route declared before the specific one. Run route:list, compare the verb, then fix the form or the route.

  • Applies to Laravel 12, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the routes for the URL and read the Method column. The verb of your request must be one of them. The Allow header of the 405 response lists them too.

    php artisan route:list --path=login
  2. 2Match what you see, then jump to its fix

    1. 1The route list shows PUT|PATCH or DELETE for the URL and the form uses method="POST".A form sends POST but the route is PUT, PATCH or DELETEFix
    2. 2The request verb is not one of the verbs route:list shows for that URI.The request uses a verb the route was not declared withFix
    3. 3route:list shows two routes for the same URI, and the specific one is below the catch-all.A route with any, match or redirect shares the URI and comes firstFix

What does the error look like?

  • 405 Method Not Allowed
  • HTTP/1.1 405 Method Not Allowed

Cause 1 of 3

A form sends POST but the route is PUT, PATCH or DELETE

1Check

HTML forms only support GET and POST, so another verb must be sent as a hidden _method field.

grep -rn "<form" resources/views | head

2Fix

Keep method POST on the form and add the @method directive, together with @csrf.

<form action="/posts/1" method="POST">
    @method('PUT')
    @csrf
</form>

3Verify

Submit the form again; the route now matches.

php artisan route:list --path=posts

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The request uses a verb the route was not declared with

1Check

Typing a POST-only URL in the address bar sends GET. A fetch call may also use the wrong method.

php artisan route:list -v --path=api

2Fix

Send the verb the route supports, or declare the route for the verb you need. Route::match covers a list of verbs, Route::any covers all.

Route::match(['get', 'post'], '/contact', [ContactController::class, 'handle']);

3Verify

Repeat the request with the intended verb and read the status line.

curl -i -X POST https://example.com/contact

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

A route with any, match or redirect shares the URI and comes first

1Check

Laravel's docs say routes using get, post, put, patch, delete or options must be defined before any, match or redirect routes with the same URI.

php artisan route:list --path=orders

2Fix

Move the specific verb routes above the broad one in routes/web.php or routes/api.php.

Route::post('/orders', [OrderController::class, 'store']);
Route::any('/orders', fn () => abort(404));

3Verify

Run route:list again and call the URL with the intended verb.

php artisan route:list --path=orders

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Read the Method column of route:list whenever a new endpoint is added.
  • Always pair @method with @csrf in forms that are not plain GET or POST.
  • Declare specific verb routes before any, match or redirect routes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.