Did you setup any foreign keys in your database schemas? If so you can set them to cascade on delete.
Laravel Delete all with relation?
Hello, I'm trying to figure out how I can delete everything that is connected to a theme, Currently, I have a topic which has replies and to delete those 2 from the database I do something like this in the destroy method
$topic = Topic::find($id);
$topic->replies()->delete();
$topic->delete();
return redirect('/');
But a topic is within a theme. So what I'm trying to do is, add a function which allows the user to delete a theme, But that function also needs to delete ALL the topics within that theme and then ALL the replies of all those topics.
I maybe figured that it needs to look something like this but I can be completely wrong.
$theme = Theme::find($id);
$theme->topics()->replies->delete();
$theme->delete();
return redirect('/')
The relations between all the things are okay but I can list them here, A theme has many Topics A Topic has many replies
How do I do this? Thanks in advance
PS: Sorry for the weird title, I don't know how to explain this (Tips always appreciated)
Please or to participate in this conversation.