On this kind of situation, depending on the context, I like to use soft deletes.
The deleted institutions will not be shown anymore but the db stay coherent.
What is the best way to delete related records (models) in Laravel? I've read the documentation but there's very little in there about deleting records. Also searched elsewhere, but I see lots of different solutions, which mostly look pretty ugly.
In my project I have different models with several oneToMany and ManyToMany relations. For example:
Institution
- hasMany Project (table projects)
- hasMany User (table users)
- etc
Project
- hasMany Contribution (table projectcontributions)
- belongsToMany Animalgroup (table project_animalgroups)
- belongsToMany Category (table project_categories)
- belongsToMany Institution (table project_institutions)
- etc
User
- belongsTo Institution
Now at the moment I have in my Project model a method delete with several detach() calls to delete the many to many relations records. There are also some FOREIGN KEY ON DELETE CASCADE restraints.
But what if I want to delete an Institution? Besides the Institution itself, also all projects AND relations of projects have to be deleted. And the users belonging to the institution. Should I try to set all Foreign Key ON DELETE CASCADEs in the db tables or is there an elegant way in Laravel?
Please or to participate in this conversation.