dongsuoxi1790 2017-11-09 15:54
浏览 93

laravel复选框插入mysql数据库'完整性约束违规'

I created a simple form that sends some data to a mysql database. When entering name or email everything workes fine. Except for the checkboxes. It runs perfekt when all checkboxes are checked, but if not -> i always get this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'checkbox1' cannot be null

so i got this for creating the form:

{{Form::label('checkboxlabel', 'Agree')}}
{{Form::checkbox('checkbox1',1, true)}}

This is my migration file:

 Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email')->unique();
        $table->boolean('checkbox1')->nullable();

And this my Controller file:

    $user = new User;
    $user->email= $request->input('email');
    $user->checkbox1= $request->input('checkbox1');

Any help on this? Just want some true or false value for checked or not checked. Thx guys!

  • 写回答

2条回答 默认 最新

  • dsgdsf12312 2017-11-09 16:10
    关注

    If you do not explicitly need a null value for a boolean you can do:

    // Inside your migration
    $table->boolean('checkbox1')->default(false);
    
    // Inside the controller
    $user->checkbox1 = $request->input('checkbox1');
    

    Alternatively you can set a default if the value is not set in the request:

    // Inside your migration
    $table->boolean('checkbox1');
    
    // Inside the controller
    $user->checkbox1 = $request->input('checkbox1', 0);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题