我正在使用Laravel Collective HTML,并且正在创建如下表单:
{!! Form::model($band, ['method' => 'PATCH','route' => ['bands.update', $band->id]]) !!}
<div class="row">
<div class="col-xs-4 col-sm-4 col-md-4">
...
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Still active?:</strong>
{!! Form::checkbox('still_active', null, array('class' => 'form-control')) !!}
</div>
</div>
...
</div>
</div>
{!! Form::close() !!}
它工作正常,这意味着如果still_active
来自 DB 的值是1
复选框,则显示为选中或未选中,如果值为0
. 如果我检查并选择并尝试保存元素,则会收到以下错误:
QueryException in Connection.php line 770:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'on' for column 'still_active' at row 1 (SQL: update `bands` set `start_date` = 2016-12-26, `still_active` = on, `updated_at` = 2016-12-26 22:58:19 where `id` = 47)
我检查了发送到服务器的表单数据,我可以看到still_active
设置on
为1
:
_method:PATCH
_token:PtVu9DCMcV0EXlJRAKxMOOkobNWL2O2bbKhNWRtV
name:Nayeli Hoppe
start_date:2016-12-26
website:https://bogisich.com/accusamus-sequi-aspernatur-et.html
still_active:on
这是在Controller上处理数据的方法:
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'start_date' => 'required',
'website' => 'required',
]);
Band::find($id)->update($request->all());
return redirect()->route('bands.index')
->with('success', 'Band updated successfully');
}
我怎样才能避免这种情况?在保存之前是否需要转换控制器上的数据?