2013-09-16 85 views
5

Hiện tại tôi đang làm việc trên một dự án để quản lý chi phí nhiên liệu của mình. Bây giờ tôi cố gắng vượt qua 2 tham số trong một Form::open() mà thật đáng buồn không hoạt động. Lý do tại sao tôi nghĩ rằng tôi cần phải vượt qua 2 thông số cùng một lúc là vì url của tôi là Sitename/car/{id}/tank/{id} Tôi đang làm gì sai?Biểu mẫu Laravel 4 chuyển 2 tham số

edit.blade.php

Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('[email protected]', array($aid, $id)))) 

Vấn đề Mã

'action' => array('[email protected]', array($aid, $id) 

-Results trong các lỗi sau:

Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.


TankController.php

public function edit($id, $tid) 
{ 
    $tank = Tank::find($tid); 
    if(!$tank) return Redirect::action('[email protected]'); 
    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('[email protected]', $id)->with('success', 'Tankbeurt succesvol aangepast'); 
    } else return Redirect::action('[email protected]', $id)->withErrors($validation); 
} 

Route.php

Route::resource('car', 'CarController'); 
Route::resource('car/{id}/tank', 'TankController'); 
Route::controller('/', 'UserController'); 

Cấu trúc -URL SITENAME/xe/2/bể/2/chỉnh sửa

Tôi cũng đã xem xét các tài liệu api nhưng không tìm thấy gì cả. http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html

Cảm ơn trước

Trả lời

16

Hãy thử điều này:

Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('[email protected]', $aid, $id))) 
3

Đây là giải pháp tốt nhất

Form::open (array ('method'=>'put', 'route'=>array($routeName [,params...])) 

Một ví dụ:

{{ Form::open(array('route' => array('admin.myroute', $object->id))) }} 

https://github.com/laravel/framework/issues/491

+1

này cũng hoạt động trên Laravel 5.1. Muchas gracias. – racl101

+0

vâng, đây là cách tốt nhất. Cảm ơn –

+0

Nhưng bạn chỉ chuyển một tham số trong ví dụ của mình. Vượt qua hai tham số theo cách đó không hoạt động trong 5.2. –

0

Cố gắng sử dụng trong lưỡi của bạn:

For Laravel 5.1

{!! Form::open([ 
      'route' => ['car.tank.update', $car->id, $tank->id], 
      'method' => 'put', 
      'class' => 'form-horizontal' 
       ]) 
!!} 
+0

Trong Laravel 5.x, lớp biểu mẫu bị loại bỏ. Nếu bạn vẫn muốn sử dụng trình trợ giúp HTML này như 'Biểu mẫu', bạn có thể sử dụng lớp thay thế cho cộng đồng [laravelcollective HTML] (https://laravelcollective.com/docs/master/html).Nguồn: [Tìm kiếm Form & HTML Helpers] (https://laravel.com/docs/master/upgrade#upgrade-5.0) – SaschaDens

+0

Không hoạt động trong Laravel Collective 5.2. –