laraveleloquentormforeign-keyssoft-delete

How to solve SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed error when recording deleted model data?


Suppose we have a project with Laravel framework and there is a scenario where we have to record/save our Product Model(for example)'s behaviour. Everything is fine while we are recording create and update actions BUT when we reach to delete action, then we can not save anything about the deleted model data!

So if we try to do save the deleted model data, we will get the following error : SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed.

So what is the solution in this kind of situations?


Solution

  • The Solution is using Soft Deletes. And here is the code example with explanation: