The POST method is not supported for this route. Supported methods: GET, HEAD
The Laravel error: “The POST method is not supported for this route. Supported methods: GET, HEAD” occurs when you send a post request to a route that is defined with a ‘get’ method. To solve this error, change ‘get’ method and define the route using ‘post’ or ‘any’ methods.
Change ‘get’ method to ‘post’ or ‘any’
The first step to solve the error is to change get
method to post
or any
method for the particular route in the routes file.
For explaining this in detail, consider the below HTML form with POST submission.
<form action="/save" method="POST">
@csrf
<input type="text" name="name"/>
<button type="submit">Save</button>
</form>
In the routes file, /save
route should be defined as post method as given below.
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// This line define '/save' routes as post method
Route::post('/save', [App\Http\Controllers\HomeController::class, 'save'])->name('save');
In the POST request, you should have csrf_token in the body to prevent csrf attack. Learn more about csrf token.
Another option is to define the route as any
method.
Route::any('/save', [App\Http\Controllers\HomeController::class, 'save'])->name('save');
A route that to be respond to all HTTP verbs can be defined using the any
method.
Clear route cache
Even after you defined the route as a post
or any
method, and if the issue still exists then follow this step.
Laravel comes with route caching out of the box in which all routes are cached. It helps to reduce the amount of time it takes to register all of your application's routes.
Even after you changed the route method, there is a chance to get the same error again due to this route cache. So clear the route cache using the below command.
php artisan route:clear
In the Laravel application, all routes are defined in route files located in the route folder. All web routes are defined in routes/web.php file. These routes are assigned the middleware group for session state and CSRF protection. The routes in routes/api.php are stateless which is used to define the routes of APIs.
Learn more about routing in Laravel doc.
Conclusion
To solve the Laravel error “The POST method is not supported for this route. Supported methods: GET, HEAD”, change ‘get’ method to ‘post’ or ‘any’ method to define that route in the routes file. If the issue still exists then clear the route cache using the command ‘php artisan route:clear’.
On datainfinities.com, Read articles all around JavaScript, React, Node.js, PHP, Laravel, and Shopify.
Related Blogs
Get raw SQL query from Laravel Query Builder
Laravel Model Events and Observers Tutorial
Delete an element from an array in PHP
How to solve error: array_merge() does not accept unknown named parameters
Laravel Order by Pivot Table Field
How to display validation error messages in Laravel blade view
Laravel Cron Job Task Scheduling Tutorial
How to create multiple where clause query using Laravel eloquent
Explore All Blogs