【发布时间】:2019-04-15 05:32:41
【问题描述】:
我在尝试更新我的帖子时收到 MethodNotAllowedHttpException 错误。所以我用谷歌搜索了这个错误并找到了这个laravel throwing MethodNotAllowedHttpException,但它得到解释我需要制作路线 一个发布请求,我的表单操作通过了,但它已经是一个帖子,它一直抛出相同的错误,我无法确定错误是 web.php 还是它自己的控制器
edit.blade.php
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
@method('PUT')
<div class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="body">Body:</label>
<textarea id="body" name="body" class="form-control" rows="10">
{{
$post->body
}}
</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
@include('layouts.errors')
</form>
Web.php
Route::get('/', 'PostsController@index')->name('home');
Route::get('/posts/create', 'PostsController@create');
Route::post('/posts', 'PostsController@store');
Route::get('/posts/{post}', 'PostsController@show');
Route::get('/posts/{post}/edit', 'PostsController@edit');
Route::post('/posts/{post}/edit', 'PostsController@update');
Route::get('/posts/{post}/delete', 'PostsController@destroy');
PostsController.php (如果你想让我发布孔控制器,这是控制器之外重要的部分,让我知道)
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'body' => 'required'
]);
$post->update($request->all());
return redirect('/')->with('succes','Product updated succesfully');
}
【问题讨论】:
-
没有为
PUT方法定义路由,您通过@method('PUT')用PUT覆盖表单的POST。更改现有定义或添加:Route::put('/posts/{post}/edit', 'PostsController@update'); -
我在 web.php 中修改了它,谢谢
标签: php laravel laravel-5.7