当我存储帖子时,我会遇到这个错误

MethodNotAllowedHttpException in RouteCollection.php line 219:

什么会导致这个问题??

路由.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');

我用的是Laravel 5.1,我一天都搞不懂..

推荐答案

由于您将帖子更新的方法设置为patch,请确保您open your form使用该方法:

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

如果你没有使用Form类,你也可以确保表单下面有一个hidden element called _method:

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

类似地,如果要通过AJAX发送此数据,只需在通过POST发送请求之前,将_method键添加到设置为'PATCH'的有效负载中.一些浏览器(IE 7/8)不支持通过XMLHttpRequest修补HTTP

您的另一个 Select 是更改路由以接受POST数据:

Route::post('posts/store-new-post', 'PostsController@store');
Route::post('posts/{slug}', 'PostsController@update');

Laravel相关问答推荐

保存很多模型太慢

如何解决此 Backblaze B2 S3 兼容 API 错误?

从主类别模型 laravel 中获取子类别翻译

使用枢轴插入多对多时的Laravel问题

如何在 laravel 5.8 中显示自定义消息?

Laravel 附加额外字段

如何在 Laravel 4 中使用没有 id 的 Eloquent 更新数据

Laravel 5 - ErrorException 无法打开流:权限被拒绝

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

Laravel 框架类在 PHPUnit 数据提供程序中不可用

laravel 中的 Http Post 使用 fetch api 给出 TokenMismatchException

在 Laravel 的 Homestead 中运行 PHPUnit

Laravel 5.4 有时验证规则不起作用

Laravel 无法创建根目录

Laravel 迁移 - 删除列

将 Laravel 集合附加到另一个集合

Laravel Nova 显示用户友好的资源名称

Laravel s3 多桶

使用迁移更改表 Laravel 5

有没有办法让表名自动添加到 Eloquent 查询方法中?