Have you ever encountered an issue where it says: GET Method is not supported for this route? Or in fact, any of the following issues:
This is mainly caused by using incorrect methods when submitting a form or inaccurate redirects. Let's take a look at examples and how to fix them.
Error 1. GET Method is not Supported for this Route
This error may appear in a few cases.
Reason 1. Redirect to Route that accepts POST only
For example, you have a route that accepts POST only:
routes/web.php
Route::post('/posts', 'PostController@store')->name('posts');
Yet, in your Controller, you are trying to redirect the user to this Route using the GET method:
Controller
public function store($request) { // ... return redirect()->route('posts');}
This will indeed cause an error. Fixing this could be as simple as using a different route that accepts the GET method:
routes/web.php
Route::get('/posts', 'PostController@index')->name('posts');Route::post('/posts', 'PostController@store')->name('posts.store');
That's it, the issue is fixed!
Reason 2. Submitting a form with GET but route accepts POST
For example, you have a form that looks like this:
resources/views/posts/create.blade.php
<form action="{{ route('posts') }}"> <!-- ... --></form>
And the route that accepts POST only:
routes/web.php
Route::post('/posts', 'PostController@store')->name('posts')
In HTML, the default method for the form submit is GET. So, if you forget to specify the method altogether, the default one will be chosen.
This will also cause an error and you should adjust your form to use the POST method instead:
resources/views/posts/create.blade.php
<form action="{{ route('posts') }}" method="POST"> <!-- ... --></form>
Error 2. POST Method is not Supported for this Route
With POST errors we usually have one of two cases.
Reason 1. Route accepts the GET method only
In this case, you should check if the route definition is correct:
routes/web.php
Route::get('/posts', 'PostController@index')->name('posts');Route::post('/posts', 'PostController@store')->name('posts.store');
In our example, we called the route posts
in our form action, yet we should have called posts.store
instead:
resources/views/posts/create.blade.php
<form action="{{ route('posts') }}" method="POST"><form action="{{ route('posts.store') }}" method="POST"> <!-- ... --></form>
Reason 2. Route accepts PUT, PATCH, and DELETE methods
In this case, you should check if you are using the correct method. For example, you have a route that accepts the PUT method only:
routes/web.php
Route::put('/posts/{post}', 'PostController@update')->name('posts.update');
And attempting to save a resource using a form like this:
resources/views/posts/edit.blade.php
<form action="{{ route('posts.update', $post) }}" method="POST"> <!-- ... --></form>
This will result in probably the most common issue that happens in Laravel:
So the solution is to always remember to add @method('PUT')
or @method('PATCH')
or @method('DELETE')
in your forms.
resources/views/posts/edit.blade.php
<form action="{{ route('posts.update', $post) }}" method="POST"> @method('PUT') <!-- ... --></form>
That's it - the issue is resolved! And it's the same basic principle for PATCH and DELETE methods.
As you can see, there are many different ways this issue could pop into your screen. And the majority of them are either typos or we just forgot to use the correct method.
Bonus: Use Route Names Instead of URLs
Using clear route names instead of URLs can help you prevent a lot of problems. This is because URLs can be both GET/POST, yet point to different routes. For example, you could have a route that accepts GET only:
routes/web.php
Route::get('/posts', 'PostController@index')->name('posts');
You can reach the route in two ways:
-
url('/posts')
-
route('posts')
And both of them will work just fine! But what if you have another route that handles the POST method?
routes/web.php
Route::get('/posts', 'PostController@index')->name('posts');Route::post('/posts', 'PostController@store')->name('posts.store');
Now, if you try to use url('/posts')
in your form action:
resources/views/posts/create.blade.php
<form action="{{ url('/posts') }}" method="POST"> <!-- ... --></form>
You will get an error:
This is because url('/posts')
will always point to the GET route, while route('posts.store')
will point to the POST route. So, if you use route names instead of URLs, you will be able to avoid a lot of issues:
resources/views/posts/create.blade.php
<form action="{{ route('posts.store') }}" method="POST"> <!-- ... --></form>
No comments or questions yet...