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

rojonunoo's avatar

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (sas.fees, CONSTRAINT fees_student_id_foreign FOREIGN KEY (student_id) REFERENCES students (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into fees (amount, payment_method, bank_name, updated_at, created_at) values (10.00, Alipay, 12, 2017-01-26 16:03:31, 2017-01-26 16:03:31))

public function store(Request $request,$id) {

    $student = Student::FindorFail($id);
    $fees = new Fees();
    $fees->student_id= $student;
    $fees->payment_method = $request['payment_method'];
    $fees->bank_name = $request['bank_name'];
    $fees->account_no = $request['account_no'];
    $fees->amount = $request['amount'];
    $fees = Fees::create($request->all());
    return $fees;

}

public function up()

{ Schema::create('students', function (Blueprint $table) {

        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name');
        $table->date('dob');
        $table->string('last_school_attended');
        $table->string('avatar')->default();
        $table->decimal('student_fees');
        $table->softDeletes('deleted_at');
        $table->timestamps();

    });
}

public function up() {

    Schema::create('fees', function (Blueprint $table) {

        $table->increments('id');
        $table->unsignedInteger('student_id')->unsigned();
        $table->string('payment_method');
        $table->string('bank_name')->nullable();
        $table->string('account_no')->nullabe();
        $table->decimal('balance');
        $table->decimal('amount');
        $table->date('date');
        $table->timestamps();

        $table->foreign('student_id')
            ->references('id')->on('students')
            ->onDelete('cascade')
            ->onUpdate('cascade');
    });

don;t know why i keep getting this error .. please help me out .. Thanks in Advance

0 likes
4 replies
rojonunoo's avatar

@HasnBatal

protected  $fillable = ['student_id','payment_method','bank_name','account_no','balance','amount','date'];

they are already set

HasnBatal's avatar
Level 5

$student = Student::FindorFail($id); $fees = new Fees(); $fees->student_id= $student->id; $fees->payment_method = $request['payment_method']; $fees->bank_name = $request['bank_name']; $fees->account_no = $request['account_no']; $fees->amount = $request['amount']; $fees->save(); return $fees;

}

@rojonunoo

Please or to participate in this conversation.