[英]Laravel Form Model Binding not submitting
I'm having some trouble using form model binding with L4. 我在使用L4的表单模型绑定时遇到了一些麻烦。 My form is being populated, and the routes are correct but it's not submitting correctly.
我的表格正在填充,路线正确,但提交不正确。
Controller: 控制器:
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();
}
View: 视图:
@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
I'm assuming that your transactions.*
routes are being generated via Route::resource()
. 我假设您的
transactions.*
路由是通过Route::resource()
生成的。
Per the documentation , Laravel generates the following routes for a resource: 根据文档 ,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
You'll see that resource.update
is expecting a PUT/PATCH
request, but Laravel forms default to POST
. 您将看到
resource.update
期待一个PUT/PATCH
请求,但是Laravel表单默认为POST
。
To fix this, add 'method' => 'PUT'
to the array of form options, like so: 要解决此问题,请将
'method' => 'PUT'
到表单选项数组中,如下所示:
{{ Form::model($transaction, array(
'method' => 'PUT',
'route' => array('transactions.update', $transaction->id)
)); }}
This will add a hidden input, <input type="hidden" name="_method" value="PUT" />
, to your form which tells Laravel to spoof the request as a PUT
. 这会将隐藏的输入
<input type="hidden" name="_method" value="PUT" />
到您的表单中,这告诉Laravel将请求欺骗为PUT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.