错误在laravel中不起作用,并给我数据库中的列错误

时间:2019-04-14 11:00:21

标签: laravel

我有一些用于插入数据的输入。当我填满所有输入后,它便能正常工作,但是当我尝试插入空输入时,它给了我这个错误违反完整性约束:1048列'monday'不能为空

现在,我也请求此表格并提供所需的所有内容,并尝试显示错误,但那也不起作用。我不知道我的代码有什么问题。下面是我的代码。查看它:)

//控制器

public function store(Request $request)
    {
        $timetable = $request->all();
        Timetable::create($timetable);
        return redirect()->back();
    }

//查看

<div class="ml-4 mr-4 py-3">

        {!! Form::open(['method'=>'POST', 'action'=>'TimetableController@store', 'class' => 'mb-5']) !!}

        <div class="form-group">
            {!! Form::label('class_id', 'Class', ['class'=>'control-label display-4']) !!}
            {!! Form::select('class_id', [''=>'Choose Class'] + $classes, null,  ['class'=>'form-control']) !!}
        </div>

        <div class="row">
            <div class="col-md-12">
                <table class=" table table-bordered table-hover table-sortable">
                    <thead>
                        <tr>
                            <th class="text-center">Period</th>
                            <th class="text-center">Monday</th>
                            <th class="text-center">Tuesday</th>
                            <th class="text-center">Wednesday</th>
                            <th class="text-center">Thursday</th>
                            <th class="text-center">Friday</th>
                            <th class="text-center">Saturday</th>

                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td style="width: 10%;">
                                {!! Form::text('period', null, ['class'=>'form-control', 'placeholder' => 'Period name']) !!}
                            </td>
                            <td style="width: 15%;">
                                    {!! Form::textarea('monday', null, ['class'=>'form-control', 'rows'=>2, 'placeholder' => 'Class timing, Teacher name, Subject name']) !!}
                            </td>
                            <td style="width: 15%;">
                                {!! Form::textarea('tuesday', null, ['class'=>'form-control', 'rows'=>2, 'placeholder' => 'Class timing, Teacher name, Subject name']) !!}
                            </td>
                            <td style="width: 15%;">
                                {!! Form::textarea('wednesday', null, ['class'=>'form-control', 'rows'=>2, 'placeholder' => 'Class timing, Teacher name, Subject name']) !!}
                            </td>
                            <td style="width: 15%;">
                                {!! Form::textarea('thursday', null, ['class'=>'form-control', 'rows'=>2, 'placeholder' => 'Class timing, Teacher name, Subject name']) !!}
                            </td>
                            <td style="width: 15%;">
                                {!! Form::textarea('friday', null, ['class'=>'form-control', 'rows'=>2, 'placeholder' => 'Class timing, Teacher name, Subject name']) !!}
                            </td>
                            <td style="width: 15%;">
                                {!! Form::textarea('saturday', null, ['class'=>'form-control', 'rows'=>2, 'placeholder' => 'Class timing, Teacher name, Subject name']) !!}
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        {{--<a class="fas fa-folder-open btn btn-success float-right mb-4"> Save</a>--}}
        {!! Form::button(' Save', ['type'=>'submit', 'class'=>'fas fa-folder-open btn btn-success float-right mb-4']) !!}
        {{--<a id="add_row" class="fas fa-plus-square btn btn-warning float-right mb-4 mr-2"> Add Row</a>--}}

        @include('includes.form_errors')

        {!! Form::close() !!}

    </div>

1 个答案:

答案 0 :(得分:1)

嗯,错误很明显。 返回迁移,找到monday列,然后添加->nullable()

示例: $table->string('monday')->nullable();