Hasnain Kahn
Hasnain Kahn

Reputation: 119

Errors not working in laravel and giving me column error in the database

I have some inputs for inserting data. When I fill all the inputs then its working successfully but when i am trying to insert the empty inputs then it give me this error Integrity constraint violation: 1048 Column 'monday' cannot be null

Now I also make request for this form and make everything required and trying to show up the error but thats also not working. I dont know whats wrong with my code. Below is my code. Review it pls :)

//Controller

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

//View

<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>

Upvotes: 0

Views: 100

Answers (1)

Benjamin Beganović
Benjamin Beganović

Reputation: 1168

Well, the error is obvious. Back to your migrations, and find the monday column, and add ->nullable().

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

Upvotes: 1

Related Questions