简体   繁体   中英

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - laravel

I have 3 tables. 1. Subject 2. teacher 3. courses

my course table migration here.

public function up()
{
    Schema::create('courses', function(Blueprint $table) {
        $table->increments('c_id');
        $table->integer('sub_id1')->unsigned()->nullable();
        $table->integer('t_id1')->unsigned()->nullable();
        $table->integer('sub_id2')->unsigned()->nullable();
        $table->integer('t_id2')->unsigned()->nullable();
        $table->integer('sub_id3')->unsigned()->nullable();
        $table->integer('t_id3')->unsigned()->nullable();
        $table->integer('sub_id4')->unsigned()->nullable();
        $table->integer('t_id4')->unsigned()->nullable();
        $table->integer('sub_id5')->unsigned()->nullable();
        $table->integer('t_id5')->unsigned()->nullable();
        $table->integer('sub_id6')->unsigned()->nullable();
        $table->integer('t_id6')->unsigned()->nullable();
        $table->integer('sub_id7')->unsigned()->nullable();
        $table->integer('t_id7')->unsigned()->nullable();
        $table->integer('sub_id8')->unsigned()->nullable();
        $table->integer('t_id8')->unsigned()->nullable();
        $table->integer('sub_id9')->unsigned()->nullable();
        $table->integer('t_id9')->unsigned()->nullable();
        $table->integer('sub_id10')->unsigned()->nullable();
        $table->integer('t_id10')->unsigned()->nullable();
        $table->integer('sub_id11')->unsigned()->nullable();
        $table->integer('t_id11')->unsigned()->nullable();
        $table->integer('sub_id12')->unsigned()->nullable();
        $table->integer('t_id12')->unsigned()->nullable();
        $table->integer('sub_id13')->unsigned()->nullable();
        $table->integer('t_id13')->unsigned()->nullable();
        $table->integer('sub_id14')->unsigned()->nullable();
        $table->integer('t_id14')->unsigned()->nullable();
    });
}

public function down()
{
    Schema::drop('courses');
}

now i'm trying to insert into the courses table. if I leave some value to null(such as sub_id5 to sub_id14 & t_id5 to t_id14) error occurs. although when I do the same thing from xampp/phpmyadmin it doesnot show any problem. the constraints on the courses table are set to no action in case of update and delete. I am using 'Collective\\Html\\FormFacade' for taking input. here is the forms code.

{!! Form::open(array('route' => 'course.store', 'method' => 'POST')) !!}
<ul>
    <li>
        {!! Form::label('sub_id1', 'Sub_id1:') !!}
        {!! Form::number('sub_id1') !!}
    </li>
    <li>
        {!! Form::label('t_id1', 'T_id1:') !!}
        {!! Form::number('t_id1') !!}
    </li>
    <li>
        {!! Form::label('sub_id2', 'Sub_id2:') !!}
        {!! Form::number('sub_id2',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id2', 'T_id2:') !!}
        {!! Form::number('t_id2',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id3', 'Sub_id3:') !!}
        {!! Form::number('sub_id3',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id3', 'T_id3:') !!}
        {!! Form::number('t_id3',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id4', 'Sub_id4:') !!}
        {!! Form::number('sub_id4',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id4', 'T_id4:') !!}
        {!! Form::number('t_id4',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id5', 'Sub_id5:') !!}
        {!! Form::number('sub_id5',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id5', 'T_id5:') !!}
        {!! Form::number('t_id5',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id6', 'Sub_id6:') !!}
        {!! Form::number('sub_id6',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id6', 'T_id6:') !!}
        {!! Form::number('t_id6',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id7', 'Sub_id7:') !!}
        {!! Form::number('sub_id7',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id7', 'T_id7:') !!}
        {!! Form::number('t_id7',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id8', 'Sub_id8:') !!}
        {!! Form::number('sub_id8',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id8', 'T_id8:') !!}
        {!! Form::number('t_id8',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id9', 'Sub_id9:') !!}
        {!! Form::number('sub_id9',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id9', 'T_id9:') !!}
        {!! Form::number('t_id9',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id10', 'Sub_id10:') !!}
        {!! Form::number('sub_id10',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id10', 'T_id10:') !!}
        {!! Form::number('t_id10',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id11', 'Sub_id11:') !!}
        {!! Form::number('sub_id11',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id11', 'T_id11:') !!}
        {!! Form::number('t_id11',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id12', 'Sub_id12:') !!}
        {!! Form::number('sub_id12',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id12', 'T_id12:') !!}
        {!! Form::number('t_id12',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id13', 'Sub_id13:') !!}
        {!! Form::number('sub_id13',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id13', 'T_id13:') !!}
        {!! Form::number('t_id13',NULL) !!}
    </li>
    <li>
        {!! Form::label('sub_id14', 'Sub_id14:') !!}
        {!! Form::number('sub_id14',NULL) !!}
    </li>
    <li>
        {!! Form::label('t_id14', 'T_id14:') !!}
        {!! Form::number('t_id14',NULL) !!}
    </li>
    <li>
        {!! Form::submit() !!}
    </li>
</ul>
  {!! Form::close() !!}

so it means if I don't put any value, in any input field it should send NULL by default. further inspecting I saw that it is not sending NULL instead it is sending ''.

here it is what I found.

at Connection->runQueryCallback('insert into `courses` (`sub_id1`, `sub_id2`, `sub_id3`, `sub_id4`, `sub_id5`, `sub_id6`, `sub_id7`, `sub_id8`, `sub_id9`, `sub_id10`, `sub_id11`, `sub_id12`, `sub_id13`, `sub_id14`, `t_id1`, `t_id2`, `t_id3`, `t_id4`, `t_id5`, `t_id6`, `t_id7`, `t_id8`, `t_id9`, `t_id10`, `t_id11`, `t_id12`, `t_id13`, `t_id14`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', array('1', '1', '2', '', '', '', '', '', '', '', '', '', '', '', '1', '1', '2', '', '', '', '', '', '', '', '', '', '', ''), object(Closure)) in Connection.php line 629

can any body help ?? Thanks in advance

From what it looks like you are using $model->fill($yourInput).

When the form is being submitted you should filter the submission data and do not include empty fields. You can do that by filtering array, for example:

$model->fill(array_filter(\Input::all(), function($var) {
   return $var !== '';
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM