Laravel 从表单传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28028996/
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 Passing Variable from Forms
提问by Benji40
I am brand new to Laravel, and following a super basic tutorial. However the tutorial did not come with an edit record section, which I am attempting to extend myself.
我是 Laravel 的新手,并且遵循了超级基础教程。然而,本教程没有附带编辑记录部分,我正在尝试扩展自己。
Route:
路线:
Route::controller('admin/products', 'ProductsController');
Controller:
控制器:
class ProductsController extends BaseController
{
public function getUpdate($id)
{
$product = Product::find($id);
if ($product) {
$product->title = Input::get('title');
$product->save();
return Redirect::to('admin/products/index')->with('message', 'Product Updated');
}
return Redirect::to('admin/products/index')->with('message', 'Invalid Product');
}
..ECT...
I realise the controller is requesting an ID to use, but I cannot figure out how to pass it a product ID when the form is posted/get.
我意识到控制器正在请求一个要使用的 ID,但是我不知道如何在发布/获取表单时将产品 ID 传递给它。
Form:
形式:
{{Form::open(array("url"=>"admin/products/update",'method' => 'get', 'files'=>true))}}
<ul>
<li>
{{ Form::label('title', 'Title:') }}
{{ Form::text('title') }}
{{ Form::hidden('id', $product->id) }}
..ECT...
{{ Form::close() }}
my initial idea was to pass the product id within the form URL like:
我最初的想法是在表单 URL 中传递产品 ID,例如:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
But no luck with that either.
但也没有运气。
The error I get is:
我得到的错误是:
Missing argument 1 for ProductsController::postUpdate()
Interestingly if I type directly into the URL:
有趣的是,如果我直接输入 URL:
http://localhost/laravel/public/admin/products/update/3
It works and the item with id 3 is altered fine.
它可以正常工作,并且 ID 为 3 的项目更改得很好。
So can anyone help and inform me how to pass the id with a form?
那么任何人都可以帮助并告诉我如何通过表单传递 id 吗?
Thanks very much
非常感谢
回答by ITroubs
The first Problem here ist the following:
这里的第一个问题如下:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
the {{product->id}}
is wrong in two ways:
的{{product->id}}
是错误的方法有两种:
- it should be
{{$product->id}}
- BUT it wouldn't work anyway because the inner {{..}} inside of the {{Form::...}} won't be recognized since it is inside a string and therefore part of the string itself.
- 它应该是
{{$product->id}}
- 但是无论如何它都不起作用,因为 {{Form::...}} 内部的内部 {{..}} 不会被识别,因为它位于字符串内部,因此是字符串本身的一部分。
You either have to write it this way:
你要么必须这样写:
{{Form::open(array("url"=>"admin/products/update/".$product->id, 'files'=>true))}}
or you give your route a name in your routes.php
file and do it this way:
或者你在你的routes.php
文件中给你的路线一个名字,然后这样做:
{{Form::open(array('route' => array('route.name', $product->id, 'files'=>true)))}}
I prefer the second way.
我更喜欢第二种方式。
You also might want to look into Form Model Bingin
您可能还想查看Form Model Bingin