Laravel 表单动作参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34913623/
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 form action parameter
提问by Zalon
I'm trying to call a controller method from a form object, to increment a given item.
我正在尝试从表单对象调用控制器方法,以增加给定的项目。
The problem is that when adding the parameter, the form action will add a question mark, instead of a slash.
问题是在添加参数时,表单动作会添加一个问号,而不是斜线。
<form method="POST" action="http://localhost/admin/pages?1">
How am I to define the parameter?
我如何定义参数?
{!! Form::open([
'action'=>['Admin\PagesController@increment', $item->id],
'style' => 'display:inline'
]) !!}
{!! Form::submit('Move Up', ['class' => 'btn btn-danger btn-xs']) !!}
{!! Form::close() !!}
回答by madawa
In you code sample, you are sending the item id as a HTTP GET parameter. You can access the item id in your controller by giving a name to the parameter as follows.
在您的代码示例中,您将项目 ID 作为 HTTP GET 参数发送。您可以通过为参数命名来访问控制器中的项目 ID,如下所示。
{!! Form::open([
'action'=>['Admin\PagesController@increment','itemId='.$item->id],
'style' => 'display:inline'
]) !!}
Then access the item id in your controller by
然后通过以下方式访问控制器中的项目 ID
Input:get('itemId')
回答by Solution
Your function shhould looks like this
你的函数应该是这样的
public function increment($id)
{
//your code;
}
And your route should have id in it with post request
你的路线应该有 id 和 post 请求
Route::post('increment/{id}','PagesController@increment');