rus*_*i88 4 crud laravel laravel-5.4
我正在建立一个博客文章来学习Laravel 5.4,我很难找到如何在任何地方更新帖子的任何例子.
我的表格如下
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
我的路线如下
Route::get('/posts/{post}/edit', 'PostsController@edit');
Route::patch('/posts/{post}', 'PostsController@update');
Run Code Online (Sandbox Code Playgroud)
我的控制器方法是
public function edit( Post $post )
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post )
{
Post::where('id', $post)->update($request->all());
return redirect('home');
}
Run Code Online (Sandbox Code Playgroud)
我收到MethodNotAllowedHTTPException错误但不确定这个部分/部分我出错了.
我假设它必须是我正在使用PATCH函数的点,或者可能只是我正在大量分配新值的方式.任何帮助将不胜感激.
Moh*_*d b 11
你应该使用
{{ method_field('PATCH') }}
Run Code Online (Sandbox Code Playgroud)
作为你的表格领域
并将行动改为
/posts/{{ $post->id }}
Run Code Online (Sandbox Code Playgroud)
像这样:
<form method="POST" action="/posts/{{ $post->id }}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18417 次 |
| 最近记录: |