使用 Laravel 提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50659992/
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
Submitting a Form with Laravel
提问by Chris Grim
I am creating an edit page for my posts. I would prefer to create an html form instead of using FORM:: so I can really learn. I am having an issue when I try to submit the data to the right controller method.
我正在为我的帖子创建一个编辑页面。我更喜欢创建一个 html 表单而不是使用 FORM:: 这样我可以真正学习。当我尝试将数据提交给正确的控制器方法时遇到问题。
The tutorial I am using says to use
我正在使用的教程说要使用
{!! Form::open(['action' => ['PostsController@update', $post->id], 'method' => 'POST'])!!}
Using my limited knowledge I tried to recreate this as
使用我有限的知识,我尝试将其重新创建为
<form action="{!! Route::post('/posts', ['PostsController@update', $post->id]) !!}" method="POST">
underneath both I am using <input name="_method" type="hidden" value="PUT">
在我正在使用的两个下面 <input name="_method" type="hidden" value="PUT">
The error I get is `"
我得到的错误是`"
Object of class Illuminate\Routing\Route could not be converted to string (View: /Users/Chris/code/chris/resources/views/posts/edit.blade.php)
类 Illuminate\Routing\Route 的对象无法转换为字符串(视图:/Users/Chris/code/chris/resources/views/posts/edit.blade.php)
My web.php file has Route::resource('posts', 'PostsController');
which as worked for everything else until now. In my contoller, my update method has
直到现在Route::resource('posts', 'PostsController');
,我的 web.php 文件都适用于其他所有内容。在我的控制器中,我的更新方法有
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
// Create Post
$post = Post::find($id);
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/')->with('success', 'Post Updated');
}
What would the correct action be to submit an update for my info?
为我的信息提交更新的正确操作是什么?
Thanks so much in advance!
非常感谢!
回答by ali
replace the form action with the following: there are many solutions:
将表单操作替换为以下内容:有很多解决方案:
1- by using action method :
1-通过使用操作方法:
<form action=" {!! action('PostsController@update',$post->id) !!}" method="POST">
2- by naming the route
2-通过命名路线
<form action=" {!! route('route-name',$post->id) !!}" method="POST">
3- by using url method
3- 通过使用 url 方法
<form action=" {!! url('/posts',$post->id) !!}" method="POST">
回答by Rwd
As Lagbox pointed out in the comments:
正如 Lagbox 在评论中指出的那样:
Route::post('/posts', ['PostsController@update', $post->id])
Is for defining the route in your routes file. To get the url you can do one of the following:
用于在路由文件中定义路由。要获取 url,您可以执行以下操作之一:
Hard code the uri
对 uri 进行硬编码
action="/posts/{{ $post->id }}"
action="/posts/{{ $post->id }}"
Use the url()
helper
使用url()
帮手
action="{{ url("posts/$post->id") }}"
or action="{{ url("post", $post->id) }}"
action="{{ url("posts/$post->id") }}"
或者 action="{{ url("post", $post->id) }}"
Use the route()
helper(This will only work if you have given the route a name)
使用route()
助手(这仅在您为路线命名时才有效)
action="{{ route('the-route-name', $post->id) }}"
action="{{ route('the-route-name', $post->id) }}"
Use the action helper
使用动作助手
action="{{ action('PostsController@update', $post->id) }}"
action="{{ action('PostsController@update', $post->id) }}"
Here is a linkto the various url helpers. My main advice here would be to mainly stick to just using one of them for a project.
这是指向各种 url 帮助程序的链接。我在这里的主要建议是主要坚持只在项目中使用其中之一。
Furthermore, your code should work absolutely fine the way it is for now but usually with REST (or the way Laravel uses rest) you would make either a PUT
or PATCH
request for updating instead of a POST
request.
However, standard html forms only support GET
and POST
so Laravel provides a way for you to spoof the form method:
此外,您的代码现在应该可以正常工作,但通常使用 REST(或 Laravel 使用 rest 的方式)您会发出PUT
或PATCH
请求更新而不是POST
请求。然而,标准的 html 表单只支持GET
,POST
所以 Laravel 为你提供了一种欺骗表单方法的方法:
<input type="hidden" name="_method" value="PUT" />
回答by Chris Grim
Thank you so much Lagbox. I used
非常感谢 Lagbox。我用了
<form action=" {!! route('route-name',$post->id) !!}" method="POST">
and it worked perfectly!
它工作得很好!