0
votes

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

2

2 Answers

1
votes

The reason why you are getting that exception is because you are inserting a record on table teachers which has the value of the dep_id. dept_id is not present on table teachers.

Table teachers is dependent on table departments. So be sure that when inserting records on table teachers, the values for dept_id already exists on the parent table departments.

1
votes

I go the same error,make sure your form is submitting necessary table column names.
Did you name your table in Model? I think its a mistake.Make it looks like this.

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