15

When I storing a post I get this error

MethodNotAllowedHttpException in RouteCollection.php line 219:

What can cause this problem ??

Routes.php:

Route::get('home', 'PostsController@index');
Route::get('/', 'PostsController@index');
Route::get('index', 'PostsController@index');

Route::get('posts', 'PostsController@index');
Route::get('post/{slug}/{id}', 'PostsController@show');
Route::get('posts/sukurti-nauja-straipsni', 'PostsController@create');
Route::patch('posts/store-new-post', 'PostsController@store');
Route::get('post/{slug}/{id}/edit', 'PostsController@edit');
Route::patch('posts/{slug}', 'PostsController@update');


Route::get('tags/{tags}', 'TagsController@show');
Route::get('categories/{categories}', 'CategoriesController@show');

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

I'm using Laravel 5.1 and I can't figure this out for a day..

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
frenchbaguette
  • 1,297
  • 6
  • 22
  • 41
  • 1
    This happens when you are sending something by post and trying to fetch it by get or the other way around. – patricio Sep 25 '15 at 15:57

6 Answers6

9

Since you're setting the method on the post's update to be patch, be sure you open your form to use that method:

{!! Form::open(['method' => 'patch']) !!}

If you're not using the Form class, you can also just ensure there's a hidden element called _method underneath the form:

<input name="_method" type="hidden" value="PATCH">

Similarly, if you're sending this data via AJAX, just add a _method key to the payload set to 'PATCH' before sending the request via POST. Some browsers (IE 7/8) do not support PATCH HTTP through XMLHttpRequest

Your other option is to change your route to accept POST data instead:

Route::post('posts/store-new-post', 'PostsController@store');
Route::post('posts/{slug}', 'PostsController@update');
Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • I am facing the same issue of my server. But on my local server it's working fine. Using L5.1 I didn't use patch already using post method via ajax. Any help – Kapil Verma Feb 17 '17 at 12:40
  • This answer just make me realize that sometimes I'm just only a stupid person. Hahaha! I'm using POST but in my route, I just forget to use Route::post() instead of Route::get(). – Blues Clues Jan 15 '18 at 03:58
1

Check your form tag

<form action="/path/" method="post">

in here "/path/" should be "/path" , do not use "/" at the end.

Ahmet Uğur
  • 462
  • 6
  • 12
1

Try adding to You Model: protected $guarded = ['_token'];

PawelW
  • 110
  • 3
  • 5
1

In my case there was a extra "/" at the end, something like: POST /api/clients/ I've deleted it and has worked: POST /api/clients

0

I was having this problem too, but in my case it turned out to be due to having those multiple routes set up to the same controller action:

Route::get('/',     'PostsController@index');
Route::get('posts', 'PostsController@index');

This worked fine for GET requests, but I'd set my form to submit to itself – ie. I hadn't specified an action on my form – which meant that if I was on /posts it worked (since I'd set up an appropriate POST endpoint for that route), but from the home page / it would always give me the MethodNotAllowedHttpException that you describe (because there was no POST data route set up for that). It took ages to figure out why the form seemed to sometimes work and sometimes not.

In the end I fixed it by changing the route for / into a redirect, like this:

Route::get('/', function(){
    return redirect('posts');
});

...although I guess explicitly setting an action on the form (or setting a POST route for / too) would have done the job too.

I'm new to Laravel, so there might well be other approaches that are better than either of the above!

Nick F
  • 9,781
  • 7
  • 75
  • 90
0

Navigate to vendor/laravel/framework/src/Illuminate/Foundation/Middleware/VerifyCsrfToken.php and add route method you want(POST,GET) within function isReading()method.

Hope this may help someone.

srivat1
  • 111
  • 3
  • 11
  • This is a part of laravel, you don't want to fiddle with it. Laravel 5.3 moved this file to another location (vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php). – Yevgeniy Afanasyev Feb 16 '17 at 05:01