I have three table, They are related to each other with the hasMany relationship.Now what i want is that if first table id=1 exist in second table foreign key and third table foreign key then it will delete all related data which have id=1.
my method to delete data:
public function destroy($id) {
UniversityModel::destroy($id);
UniversityinfoModel::where('university_id',$id)->delete();
UniversityAcademicModel::where('university_id',$id)->delete();
}
its work but when I upload it to the server after some hour it deleted all data from my database.
is it okay?
if university id=1 and when in universityinfomodel and universityacadmicmodel have foreignkey id=1 all data will be delete?
@mdeorue don't understand. I also did an eloquent query. its deleted eloquent data correctly but when I upload it live server data getting lost.
no, I don't use the foreign key.
where do I need to add deleting method?
in my university model?
and where I should add $university variable.
please let me clear.
thanks for your valuable answer.
@armancs - you can either add the code I provided to you your University class and create the observer or use @jorpedito's code without using the observer. It's personal preference. I like putting things in observer so I can isolate laravel magic in one place