Maaz Zindani

Loading the index…

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

Laravel answers a form or AJAX POST with 405 Method Not Allowed

Error405 Method Not Allowed

Short answer

A 405 in Laravel means the URL exists but not for the HTTP verb you sent. Run php artisan route:list and compare the verb with the request. Usual causes: the route is declared with get(), an HTML form sends PUT or DELETE without a _method field, or a cached route file is out of date.

  • Applies to Laravel 12, PHP 8.3
  • 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. If POST is not in it for that URI, the request never had a chance.

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

    1. 1route:list shows GET|HEAD for the URI, and POST is missingThe route is registered for a different verbFix
    2. 2The route is Route::put(), Route::patch() or Route::delete(), and the failing call comes from a <form>An HTML form sends PUT, PATCH or DELETE without the _method fieldFix
    3. 3The route exists in routes/web.php, but route:list does not show it and nothing changed after you saved the fileThe route cache was built before the route was addedFix

What does the error look like?

  • 405 Method Not Allowed
  • A POST, PUT or DELETE call returns status 405 while the same URL opens fine with GET

Cause 1 of 3

The route is registered for a different verb

1Check

Compare the verb in route:list with the method of your form or fetch call.

grep -n "orders" routes/web.php routes/api.php

2Fix

Declare the route with the verb you send. Use Route::match or Route::any only when one URI really has to accept several.

Route::post('/orders', [OrderController::class, 'store']);

// one URI, two verbs
Route::match(['get', 'post'], '/orders', [OrderController::class, 'handle']);

3Verify

route:list now shows POST, and the request returns the controller's response.

php artisan route:list --path=orders

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

An HTML form sends PUT, PATCH or DELETE without the _method field

1Check

HTML forms can only send GET or POST, so the browser posts to a PUT route and gets 405.

grep -n "<form" resources/views/orders/edit.blade.php

2Fix

Keep method="POST" on the form and add the _method field. Blade has a directive for it, and the form also needs the CSRF field.

<form action="/orders/{{ $order->id }}" method="POST">
    @csrf
    @method('PUT')
    ...
</form>

3Verify

Submit the form again: the PUT route should run.

php artisan route:list --path=orders

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

The route cache was built before the route was added

1Check

With a cached route file, new routes are ignored until the cache is rebuilt. Laravel's docs say to rebuild it when you add routes.

ls bootstrap/cache

2Fix

Clear the cache while developing, or run route:cache again as part of every deploy.

php artisan route:clear
# production, after deploy:
php artisan route:cache

3Verify

The new route shows up in route:list.

php artisan route:list --path=orders

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Run php artisan route:list after adding any route, and keep that habit in code review.
  • Put route:cache in the deploy script so the cache can never lag behind the code.
  • Use Blade's @method and @csrf in every form that is not a plain GET.
  • For AJAX calls, send the exact verb the route declares.

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.