Laravel 5.4 - 更新资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44883661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 - Updating a resource
提问by rushtoni88
I'm building a blog post to learn Laravel 5.4 and am struggling to find any examples of how to update a Post anywhere.
我正在构建一篇博文来学习 Laravel 5.4,并且正在努力寻找如何在任何地方更新帖子的任何示例。
My form is as follows
我的表格如下
<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>
My Routes are as follows
我的路线如下
Route::get('/posts/{post}/edit', 'PostsController@edit');
Route::patch('/posts/{post}', 'PostsController@update');
And my controller methods are
我的控制器方法是
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');
}
I get a MethodNotAllowedHTTPException
error but am not sure which part / parts of this I am getting wrong.
我收到一个MethodNotAllowedHTTPException
错误,但不确定我出错的部分/部分。
I'm assuming it must be the point at which I'm using the PATCH function, or potentially just the way I'm mass-assigning the new values. Any help would be greatly appreciated.
我假设它必须是我使用 PATCH 函数的点,或者可能只是我批量分配新值的方式。任何帮助将不胜感激。
回答by Mohammad b
you should use
你应该使用
{{ method_field('PATCH') }}
as your form field
作为您的表单域
and change action to
并将动作更改为
/posts/{{ $post->id }}
like this:
像这样:
<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>
回答by Rwd
There are a couple of things you have missed out.
你错过了一些事情。
Firstly, as @Maraboc pointed out in the comments you need to add method spoofing as standard HTML forms only allow for GET
and POST
methods:
首先,正如@Maraboc 在评论中指出的,您需要添加方法欺骗,因为标准 HTML 表单只允许GET
和POST
方法:
<input type="hidden" name="_method" value="PATCH">
or
或者
{{ method_field('PATCH') }}
https://laravel.com/docs/5.4/routing#form-method-spoofing
https://laravel.com/docs/5.4/routing#form-method-spoofing
Then you will also need to omit the "edit" uri in your forms action:
然后您还需要在表单操作中省略“编辑”uri:
<form method="POST" action="/posts/{{ $post->id }}">
or
或者
<form method="POST" action="{{ url('posts/' . $post->id) }}">
https://laravel.com/docs/5.4/controllers#resource-controllers
https://laravel.com/docs/5.4/controllers#resource-controllers
(scroll down a little bit to the Actions Handled By Resource Controllersection)
(向下滚动到资源控制器处理的操作部分)
You also may find it helpful to watch https://laracasts.com/series/laravel-5-from-scratch/episodes/10
您可能还会发现观看https://laracasts.com/series/laravel-5-from-scratch/episodes/10很有帮助
Hope this helps!
希望这可以帮助!
回答by user12810415
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
在构建 API 时,您可能需要一个位于 Eloquent 模型和实际返回给应用程序用户的 JSON 响应之间的转换层。Laravel 的资源类使您可以轻松地将模型和模型集合转换为 JSON。