我正在尝试更新我的帖子时收到MethodNotAllowedHttpException错误。所以我google了错误,发现这个laravel throwing MethodNotAllowedHttpException,但它得到解释,我需要使路线一个后请求,我的形式动作去thruw但它已经是一个帖子,它不断抛出相同的错误,我不知道如果错误是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');
}
你应该试试这个:
查看文件:
<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');
您正在使用put方法提交表单,因为定义@method('PUT')
将使其成为put路径。为这个Route::put('/posts/{post}/edit', 'PostsController@update');
定义一个put方法的路由,或者从你的刀片文件中删除@method('PUT')
。
这个问题花了我一个星期,但我用路由解决了这个问题
在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');
如果在表中错误地放置表单,Laravel也会抛出一个MethodNotAllowedHttpException异常:
<table><form><tr><td>...</td></tr></form><table>
而不是这个:
<table><tr><td><form>...</form></td></tr></table>