I will get MethodNotAllowedHttpException when submitting a form in laravel
Html file
<form method="POST" action="/cards/{{$card->id}}/notes">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<textarea name="body" class="form-control"></textarea>
<button type="submit">Add Note</button>
</form>
routes.php
Route::post('cards/{card}/notes','NotesController@store');
NotesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store()
{
return request()->all();
}
}
Make sure you don't have a route, say a Route::post
with a parameter that lies in front of the route you are trying to hit.
For example:
Route::post('{something}', 'SomethingController@index');
Route::post('cards/{card}/notes', 'NotesController@store');
In this case, no matter what you try to send to the cards route, it will always hit the something
route because {something}
is intercepting cards
as a valid parameter and triggers the SomethingController
.
Put the something
route below the cards route and it should work.
MethodNotAllowedHttpException
is thrown when no matching route (method and URI) was found, but a route with a matching URI but not matching method was found.
In your case, I guess the issue is because URI parameters differ between the route and the controller.
Here are two alternatives you can try:
- Remove the parameter from your route:
Route::post('cards/notes','NotesController@store');
- Add the parameter to your controller:
public function store($card)
{
return request()->all();
}
I have tried to solve this error in lumen and it took me quite a lot of time to figure out the problem.
The problem is with laravel itself.
Sometimes if you have another route like GET device/{variable}, laravel stops in this first route...
So what you need to do is change the route POST device
to POST device/add
This link helped me a lot