我在尝试更新我的帖子时遇到了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');
}
答案 0 :(得分:2)
您应该尝试以下操作:
查看文件:
<form method="POST" action="{{ route('post.update',[$post->id]) }}">
{{ csrf_field() }}
<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>
您的路线
Route::post('/posts/{post}/edit', 'PostsController@update')->name('post.update');
答案 1 :(得分:0)
您正在使用put方法提交表单,因为定义@method('PUT')
将使其成为放置路线。可以为Route::put('/posts/{post}/edit', 'PostsController@update');
这样的put方法定义路由,或者从刀片文件中删除@method('PUT')
。
答案 2 :(得分:0)
这个问题花了我一周的时间,但我通过路由解决了
在edit.blade.php中
{!! Form::open([route('post.update',[$post->id]),'method'=>'put']) !!}
<div class="form-group">
<label for="title">Title:</label>
{!! Form::text('title',$post->title,['class'=>'form-control',
'id'=>'title']) !!}
</div>
<div class="form-group">
<label for="body">Body:</label>
{!! Form::textarea('body', $post->body, ['id' => 'body', 'rows' => 10,
class => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Edit',['class'=>'btn btn-block bg-primary',
'name'=>'update-post']) !!}
</div>
{!! Form::close() !!}
@include('layouts.errors')
在Web.php中
Route::resource('posts','PostsController');
Route::get('/posts/{post}/delete', 'PostsController@destroy');
Route::put('/posts/{post}/edit',['as'=>'post.update',
'uses'=>'PostController@update']);
Route::get('/', 'PostsController@index')->name('home');
答案 3 :(得分:0)
如果将表单错误地放置在表中,Laravel也会抛出MethodNotAllowedHttpException异常:
<table><form><tr><td>...</td></tr></form><table>
代替此:
<table><tr><td><form>...</form></td></tr></table>