How to Resolve Laravel's 405 Error (Method Not Allowed)
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Laravel Articles Table of Contents Here
I wrote about how to resolve Laravel's 405 error (Method Not Allowed).
Here is the error message.
The GET method is not supported for this route. Supported methods: POST.
Development Environment
Laravel 11
PHP 8.3
Cause
Attempting to access a POST route with a GET request
Check the web.php
file.
The routing is written as follows:
Route::post('memo', [MemoController::class, 'store'])->name('memo.store');
If you try to access this POST method via a browser, the request will be sent as GET, leading to the error.
For example:
http://localhost/memo
This will cause the 405 error.
Solution
Verify if GET and POST are properly used
The specific function you are trying to implement may vary, so take a moment to calmly verify if you are using GET and POST correctly.
When Unsure Whether to Use GET or POST in Routing (web.php)
Use GET if you want to display a page
If you want to display a page, create a route using GET.
For example, if you want to create a memo list page, use GET like this:
Route::get('memo', [MemoController::class, 'index'])->name('memo.index');
Use POST if you want to input data
If the function involves users inputting data and saving something, create a route using POST.
For example, if you are creating a memo registration function, use POST like this:
Route::post('memo', [MemoController::class, 'store'])->name('memo.store');
Summary
That's all.
I explained a common mistake. I make this mistake quite often myself.
I hope this helps someone.
Popular Articles
Deploying a PHP7.4 + Laravel6 Project on AWS EC2
How to Build a Laravel 11 Development Environment with Docker (With Video)