2014-10-08 122 views
0

我在使用與L4的表單模型綁定時遇到了一些麻煩。我的表單正在填充,路線正確,但沒有正確提交。Laravel表單模型綁定未提交

控制器:

public function edit($id) 
{ 
    $transaction = Transaction::where('id', '=', $id)->get(); 
    return View::make('transaction')->with('transactions', $transaction); 

} 

public function update($id) 
{ 
    $transaction = Transaction::find($id); 
    $input = Input::all(); 
    $transaction->status = $input['status']; 
    $transaction->description = $input['description']; 
    $transaction->save(); 
} 

查看:

@foreach($transactions as $transaction) 
{{ Form::model($transaction, array('route' => array('transactions.update', $transaction->id))); }} 
{{ Form::text('description'); }} 
{{ Form::select('status', array('R' => 'Recieved', 'S' => 'Shipped', 'P' => 'Pending'), 'R'); }} 
{{ Form::submit('Submit'); }} 
{{ Form::close(); }} 
@endforeach 
+0

Route :: resource()是否生成'transactions。*'路由? – wolfemm 2014-10-08 01:41:13

+0

你是如何申報路線的?張貼在這裏。 – 2014-10-08 01:49:07

回答

4

我假設正在通過Route::resource()產生的transactions.*路線。

Per the documentation,Laravel生成以下路線的資源:

Verb  Path      Action Route Name 
GET  /resource     index resource.index 
GET  /resource/create   create resource.create 
POST  /resource     store resource.store 
GET  /resource/{resource}  show resource.show 
GET  /resource/{resource}/edit edit resource.edit 
PUT/PATCH /resource/{resource}  update resource.update 
DELETE /resource/{resource}  destroy resource.destroy 

你會看到resource.update期待一個PUT/PATCH請求,但Laravel forms default to POST

爲了解決這個問題,添加'method' => 'PUT'於形式的選項的數組,像這樣:

{{ Form::model($transaction, array(
    'method' => 'PUT', 
    'route' => array('transactions.update', $transaction->id) 
)); }} 

這將添加一個隱藏的輸入,<input type="hidden" name="_method" value="PUT" />,給您的形式告訴Laravel欺騙請求作爲PUT