Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arslan2037's avatar

Integrity constraint violation:

I am getting this error

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails                                   (`multiauth_5.2`.`students`, CONSTRAINT `students_dept_id_foreign` FOREIGN KEY (`dept_id`) REFERENCES `departments` (`dept_id`) ON DELETE CASCADE) (SQL: insert into `students` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Muhammad Arslan, m.arslan2037@gmail.com, 123456, 2016-07-14 13:18:08, 2016-07-14 13:18:08))

here is route

    Route::get('admin/addStudents', 'AdminController@student');
        Route::post('admin/addStudents', 'AdminController@addStudent');

here is controller method for this

    public function addStudent(Request $request)
        {
    $this->validate($request, [
            'roll_no' => 'required|min:1',
            'name' => 'required|min:2',
            'father_name' => 'required|min:3',
            'address' => 'required|min:3',
            'cnic' => 'required|min:13',
            'phone_no' => 'required|min:11',
            'father_phone_no' => 'required|min:11',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:6',
            'dept_id' => 'required|min:1',
        ]);
        Student::create($request->except('_token'));
        return redirect('admin/showStudents');
        }

model look like this

    protected $fillable = [
        'roll_no',
        'name',
        'father_name',
        'address',
        'cnic',
        'phone_no',
        'father_phone_no',
        'email',
        'password',
        'dept_id',
    ];

you can see in error there is no value for dept_id but in the form i enter this value you can check it in form

    <div class="form-group">
        {!! Form::label('roll_no','Roll No:') !!}
        {!! Form::text('roll_no', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('name','Name:') !!}
    {!! Form::text('name', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
     {!! Form::label('father_name','Father Name:') !!}
      {!! Form::text('father_name', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('address','Address:') !!}
        {!! Form::text('address', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
     {!! Form::label('cnic','CNIC:') !!}
     {!! Form::text('cnic', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
      {!! Form::label('phone_no','Phone No:') !!}
     {!! Form::text('phone_no', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
     {!! Form::label('father_phone_no','Father\'s Phone No:') !!}
     {!! Form::text('father_phone_no', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('email','Email:') !!}
     {!! Form::text('email', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        <label for="password">Password</label>
        <div>
            <input type="password" class="form-control" name="password">
        </div>
    </div>

    <div class="form-group">
        <label for="password_confirmation">Confirm Password:</label>
        <div>
            <input type="password" class="form-control" name="password_confirmation">
        </div>
    </div>

    <div class="form-group">
        {!! Form::label('dept_id','Department ID:') !!}
        {!! Form::text('dept_id', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-conrol']) !!}
    </div>
0 likes
22 replies
jlrdw's avatar

Make sure in model to tell it the primary key if not id. Like

protected $primaryKey = 'some_id';
// or
protected $primaryKey = 'whateverid';
Snapey's avatar

You cannot reference a foreign key in the other table if it does not exist.

Your form will allow me to enter 'abc' in the dept_id field but the foreign key constraint says that if you don't have a row in departments with key of 'abc' then you cannot insert the student.

If you want to allow the student to be assigned to a department then you should present this as a drop down list built from the departments table so that only existing values can be selected.

clear?

arslan2037's avatar

yes, clear but i am new, can you help me to do it

Snapey's avatar

Do you have any departments created?

Does your form work ok if you enter the id of one of the existing departments?

Snapey's avatar

show the schema for the departments table

arslan2037's avatar
    public function up()
{
    Schema::create('departments', function (Blueprint $table) {
        //   $table->increments('id');
        $table->engine = 'InnoDB';

        $table->string('dept_id');
        $table->string('name');
        $table->timestamps();

        $table->primary('dept_id');
    });
}
Snapey's avatar
Snapey
Best Answer
Level 122

In your controller

 $departments = Departments::orderBy('name','ASC')->get();

then pass $departments to your view.

Then on your form, instead of the dept_id text field;

    <select  id="dept_id" name="dept_id">
        <option value="" >Choose Department</option>
        @foreach($departments as $department)
            <option value='{{ $department->id }}'>{{ $department->name }}</option>
        @endforeach
    </select>
1 like
Snapey's avatar

one last problem, can you solve it for me.... please

Sorry, my crystal ball is in for repair

Snapey's avatar

What problem? Can it wait until the crystal ball is polished?

Snapey's avatar

one last problem, can you solve it for me.... please

WHAT LAST PROBLEM ?????? I'm being sarcastic because you have not said... WHAT?

arslan2037's avatar

"These credentials do not match our records."

when i am trying to do a login then get this error, and i am sure i enter correct values

arslan2037's avatar

i am using default authentication for login, but for registration i am using my own method

    public function addStudent(Request $request)
        {
            $this->validate($request, [
                'roll_no'         => 'required|min:1',
                'name'            => 'required|min:2',
                'father_name'     => 'required|min:3',
                'address'         => 'required|min:3',
                'cnic'            => 'required|min:13',
                'phone_no'        => 'required|min:11',
                'father_phone_no' => 'required|min:11',
                'email'           => 'required|email',
                'password'        => 'required|confirmed|min:6',
                'dept_id'         => 'required|min:1',

            ]);

            $data = array(
                    'roll_no'   => $_POST['roll_no'],
                    'name'      => $_POST['name'],
                    'father_name' => $_POST['father_name'],
                    'address'   => $_POST['address'],
                    'cnic'      => $_POST['cnic'],
                    'phone_no'  => $_POST['phone_no'],
                    'father_phone_no' => $_POST['father_phone_no'],
                    'email'     => $_POST['email'],
                    'password'  => bcrypt('password'),
                    'dept_id'   => $_POST['dept_id'],
                );
                Student::Insert($data);
                return redirect('admin/showStudents');
            }
Snapey's avatar

so nothing to do with integrity constraint violation?

Start a new thread?

Snapey's avatar

if you are using standard Auth, it's not going to validate against an entry in a model called Student

Please or to participate in this conversation.