违反完整性约束:1048 列 'cd_id' 在 laravel 中不能为空

Integrity constraint violation: 1048 Column 'cd_id' cannot be null in laravel

SQLSTATE[23000]:违反完整性约束:1048 列 'cd_id' 不能为空(SQL:插入 commvendordatanew_datecd_idcn_idunit_idvender1vender2vender3vender4)值(07/17/2021, ?, ?, ?, ?, ?, ?, ?))

如何使用 laravel

消除此错误

表格或blade截图

<form action="{{route('auth.commvendordata')}}" method="POST" class="shadow-lg p-4 w-f">
                    @csrf
                    <div class="modal-date">
                        <label>Select Date:&emsp;&emsp;</label>
                        <input type="text" name="new_date" placeholder="Select Price Date" class="getdate"/>
                        <button type="button" name="loadform" class="btn btn-warning load-form">Load Form</button>

                    </div>
                  <div class="data" style="display:none;">
                    <table class="table table-bordered" id="table">
                        <thead class="bg-success">
                            <tr>
                                <th scope="col" class="td-size">CCode</th>
                                <th scope="col" class="td-size">CName</th>
                                <th scope="col" class="td-size">CUnits</th>
                                <th scope="col" class="td-size">Vendor 1</th>
                                <th scope="col" class="td-size">Vendor 2</th>
                                <th scope="col" class="td-size">Vendor 3</th>
                                <th scope="col" class="td-size">Vendor 4</th>
                            </tr>
                        </thead>
                        
                            <tr id="template">

                                    <td><input type="text" name="ccode[]" class="td-size"></td>
                                    <td><input type="text" name="cname[]" class="td-size"></td>
                                    <td><input type="text" name="cunit[]" class="td-size"></td>
                                    <td><input type="text" name="vendor1[]" class="td-size"></td>
                                    <td><input type="text" name="vendor2[]" class="td-size"></td>
                                    <td><input type="text" name="vendor3[]" class="td-size"></td>
                                    <td><input type="text" name="vendor4[]" id="addrow" class="td-size"></td>
                            </tr>
                        
                    </table>
                  </div>
                  <div class="modal-footer">
                  <button type="submit" name="commdatasavebtn" class="btn btn-success">Save</button>
                </div>
                </form>

控制器截图

函数 commvendordata(请求 $request){

$cvendordata = new commvendordata;

$new_date = $request->new_date;
$ccode = $request->ccode;
$cname = $request->cname;
$cunit = $request->cunit;
$vender1 = $request->vendor1;
$vender2 = $request->vendor2;
$vender3 = $request->vendor3;
$vender4 = $request->vendor4;
    for($i=0; $i<count($ccode); $i++) {
            $commdata = [
                'new_date'  => $new_date,
                'cd_id'     => $ccode[$i],
                'cn_id'     => $cname[$i],
                'unit_id'   => $cunit[$i],
                'vender1'   => $vender1[$i],
                'vender2'   => $vender2[$i],
                'vender3'   => $vender3[$i],
                'vender4'   => $vender4[$i]
            ];
            DB::table('commvendordata')->insert($commdata);
    }
    Session::put('Success',"Save Data Successfully...!");
    return back();

}

数据库截图

错误截图

表格截图

由于您在表格的最后一行留空,因此出现错误。将数据放在最后一行,我认为问题将得到解决。在你的数据库 table.

中插入行之前还要进行一些验证

解决方案 1

更改 for 循环
for($i=0; $i<count($ccode); $i++)

for($i=0; $i<count($ccode)-1; $i++)

解决方案 2

之前

DB::table('commvendordata')->insert($commdata);

添加这个

if (trim(implode('', array_values($commdata))) == '') continue;

它将跳过所有字段为空的行(在您的例子中是最后一行)。 此外,您应该添加一些验证以确保用户始终为数据库中需要的字段输入一些值。