当我试图更新我的帖子时,我得到了一个MethodNotAllowedHttpException错误。所以我在谷歌上搜索了这个错误,找到了这个laravel throwing MethodNotAllowedHttpException,但它被解释为我需要使路由成为一个post请求,其中我的表单操作是thruw,但它已经是一个post,它一直抛出相同的错误,并且我不能确定错误是以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');
}发布于 2018-11-12 21:30:36
你应该试试这个:
查看文件
<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');发布于 2018-11-12 21:29:16
您正在使用put方法提交表单,因为定义@method('PUT')将使其成为put路由。或者像这样为put方法定义一个路由,或者从您的刀片文件中删除@method('PUT')。
发布于 2018-11-17 13:49:03
这个问题花了我一周的时间,但我用路由解决了它
在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');https://stackoverflow.com/questions/53263128
复制相似问题