Laravel 4 表单传递 2 个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18832260/
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 4 Form passing 2 parameters
提问by SaschaDens
Currently i'm working on a project to manage my cost on fuel.
Now i try to pass 2 parameters in a Form::open()
which sadly doesn't work.
The reason why i think i need to pass 2 parameters at once is because my url is Sitename/car/{id}/tank/{id}
What am i doing wrong?
目前我正在开展一个项目来管理我的燃料成本。现在我尝试在 a 中传递 2 个参数,Form::open()
但遗憾的是它不起作用。我认为我需要一次传递 2 个参数的原因是因为我的 url 是Sitename/car/{id}/tank/{id}
我做错了什么?
edit.blade.php
编辑刀片.php
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController@update', array($aid, $id))))
Problem Code
问题代码
'action' => array('TankController@update', array($aid, $id)
-Results in the following error:
-导致以下错误:
Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.
Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.
TankController.php
坦克控制器.php
public function edit($id, $tid)
{
$tank = Tank::find($tid);
if(!$tank) return Redirect::action('TankController@index');
return View::make('Tank.edit', $tank)->with('aid', $id);
}
public function update($id, $tid)
{
$validation = Validator::make(Input::all(), Tank::$rules);
if($validation->passes()){
$tank = Tank::find($tid);
$tank->kmstand = Input::get('kmstand');
$tank->volume = Input::get('volume');
$tank->prijstankbeurt = Input::get('prijstankbeurt');
$tank->datumtank = Input::get('datumtank');
$tank->save();
return Redirect::action('TankController@index', $id)->with('success', 'Tankbeurt succesvol aangepast');
} else return Redirect::action('TankController@edit', $id)->withErrors($validation);
}
Route.php
路由.php
Route::resource('car', 'CarController');
Route::resource('car/{id}/tank', 'TankController');
Route::controller('/', 'UserController');
-Url StructureSITENAME/car/2/tank/2/edit
-Url 结构SITENAME/car/2/tank/2/edit
I've also looked into the api documents but found nothing. http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html
我还查看了 api 文档,但一无所获。 http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html
Thanks in advance
提前致谢
回答by Antonio Carlos Ribeiro
Try this:
尝试这个:
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController@update', $aid, $id)))
回答by Sangar82
This is the best solution
这是最好的解决方案
Form::open (array ('method'=>'put', 'route'=>array($routeName [,params...]))
An example:
一个例子:
{{ Form::open(array('route' => array('admin.myroute', $object->id))) }}
回答by Emil Eremiev
Try to use in your blade:
尝试在您的刀片中使用:
For Laravel 5.1
对于Laravel 5.1
{!! Form::open([
'route' => ['car.tank.update', $car->id, $tank->id],
'method' => 'put',
'class' => 'form-horizontal'
])
!!}