Laravel复选框保存和更新 [英] Laravel checkbox save and update

查看:67
本文介绍了Laravel复选框保存和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中保存和更新复选框的最佳实践是什么?

What is the best practice for saving and updating a checkbox in Laravel?

我已经设置了布尔数据类型,并且在迁移文件中默认值为0,如果我尝试保存该复选框,则会出现此错误:

I have set the boolean data type and a default values is 0 in migration file and if i try to save the checkbox i get this error:

Incorrect integer value: 'on' for column

还有更新方法呢...

and what about the update method also...

这是我在查看文件中的复选框:

this is my checkbox in view file:

{!! Form::checkbox('clothing_supplied', null, isset($event) ? $event->clothing_supplied : 0, ['id' => 'check', 'class' => 'form-control' ]) !!}

这就是我的更新方式

$event->update($request->all());

推荐答案

这应该可以解决问题:

$event->clothing_supplied = ($request->get('clothing_supplied') === 'on');

这应该添加到您的控制器操作中或要更新模型的任何地方.

This should be added to your controller action or wherever you're updating your model.

之所以会这样,是因为表单提交的实际值是字符串on,而不是布尔值.

This happens because the actual value submitted by the form is the string on instead of a boolean value.

或者:

$all = $request->all();
$all['clothing_supplied'] = ($request->get('clothing_supplied') === 'on');
$event->update($all);

这篇关于Laravel复选框保存和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆