Laravel Collective HTML复选框形式值'on'而不是'1'在添加|更新DB元素时导致问题

时间:2016-12-26 23:00:42

标签: php laravel laravel-5.3 laravelcollective

我正在使用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的值为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');
}

我怎能避免这种情况?在保存之前,我是否需要转换控制器上的数据?

1 个答案:

答案 0 :(得分:0)

尝试{!! Form::checkbox('still_active', 1, null, array('class' => 'form-control')) !!}

Here是一些参考。