0
votes

I wanted to delete a user profile, when the last logout time was 2 years ago and there is no current activities were made during that time.

This is the problem encountered when i run the code:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (spwtd-test.training_user_answers, CONSTRAINT training_user_answers_training_user_id_foreign FOREIGN KEY (training_user_id) REFERENCES training_users (id))

this is the snippet code in loginController:

if ($completed = UserSyllabus::where('is_completed',1)->first())
       {$aa = UserAnswer::where('training_user_id')
        $userss = User::where('login_id', $request->login_id)
       ->where( 'last_logout', '<', Carbon::now()->subYears(2))
       ->delete();}
1

1 Answers

1
votes

You are trying to delete a User but an entry in your table UserAnswer is bind to User ( using the foreign key training_user_id ). You need to delete the UserAnswer, before deleting the User itself.