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

arslan2037's avatar

I faced a problem while inserting data into table

Please help me to solve my problem

here is controller method for insert data

    public function addTeacher(Request $request)
    {
            $this->validate($request, [
            'teacher_id' => 'required|min:1',
            'name' => 'required|min:2',
            'address' => 'required|min:3',
            'cnic' => 'required|min:13',
            'phone_no' => 'required|min:11',
            'email' => 'required|email|unique:teachers',
            'password' => 'required|confirmed|min:6',
            'dept_id' => 'required|min:1'
        ]);
        Teacher::create($request->except('_token'));
        return redirect('admin/showTeachers');
    }

here is Model

      class Teacher extends Authenticatable
    {
            protected $primaryKey = 'teacher_id';

public $incrementing = false;

protected $fillable = [
    'teacher_id',
    'name',
    'address',
    'cnic',
    'phone_no',
    'email',
    'password',
    'dept_id',
];
}

i am getting this error (SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails)

i have data in department table but still getting this error

and one more thing i have another table courses that is also dependent on department table and when i insert on courses table with same dept_id i cannot get any error

0 likes
30 replies
SaeedPrez's avatar

Please help me to solve my problem

Ok

Edit: You had just written the above text (without explaining what the problem is or providing code) when I wrote OK.

EventFellows's avatar

Your foreign key constraint fails so you might want to share your migration for the table...

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

        $table->string('teacher_id');
        $table->string('name');
        $table->string('address');
        $table->string('cnic')->unique();
        $table->string('phone_no');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('dept_id');
        $table->rememberToken();
        $table->timestamps();

        $table->primary('teacher_id');
        $table->foreign('dept_id')->references('dept_id')->on('departments')->onDelete('cascade');
    });
}
EventFellows's avatar

Without seeing your code I would guess that your request does not contain a dept_id or at least not an ID that is present on the other table.

EventFellows's avatar

Just dd($request->all()) and show what this contains. Does it come from a form?

arslan2037's avatar

I enter 1 for dept_id and i have value against 1 id

arslan2037's avatar

i get this

    array:10 [▼
    "_token" => "o4X6EWezHg9RafepofCwLDZksVwsinZ4TVdmycvP"
    "teacher_id" => "1"
    "name" => "Imran Khalil"
    "address" => "Lahore"
    "cnic" => "35402-7324378-3"
    "phone_no" => "03085049489"
    "email" => "[email protected]"
    "password" => "123456"
    "password_confirmation" => "123456"
    "dept_id" => "1"
    ]
EventFellows's avatar

make sure you also have same type. A string '1' is not the same as an integer 1 eventhough it looks the same on the screen.

EventFellows's avatar

You also might want to change your ID to type integer and make sure you mark them as ->unsigned() in your migration. then run migrate:refresh (you will loose your data)

EventFellows's avatar

You can try if ->unique() works as an attribute on you migration table. but you would probably want to re-check the documentation as your setup is quite "interesting" (=uncommon)

arslan2037's avatar

its dummy data, that is why i enter 1 but actually dept_id is something like this (12bsit01141) that why i enter string

arslan2037's avatar

One more thing i want to tell you is that, i have another table called courses that is also dependent on departments table and when i insert on courses table with same dept_id i cannot get any error

EventFellows's avatar

to the collumn of the foreign key like this

$table->string('dept_id')->unique();
EventFellows's avatar

normally the error tells you in detail what values are being inserted into which columns - check it one by one I suggest.

arslan2037's avatar

i am doing this way

    $data = array(
         'teacher_id' => $_POST['teacher_id'],
            'name' => $_POST['name'],
            'address' => $_POST['address'],
            'cnic' => $_POST['cnic'],
            'phone_no' => $_POST['phone_no'],
            'email' => $_POST['email'],
            'password' => bcrypt('password'),
            'dept_id' => $_POST['dept_id'],
        );

and instead of using

    Teacher::create($request->except('_token'));

now i am using

    Teacher::Insert($data);

and no error occur this time but new problem occur

Snapey's avatar

same problem as your other question you cannot have foreign key constraint to a record that does not exist.

arslan2037's avatar

what i can do know tell me i am new in laravel

Please or to participate in this conversation.