mercredi 12 juillet 2017

Laravel delete related data?

If have a system that works like this.

There is a theme, Within a theme, are many topics and in those topics are many replies. To delete a topic you have to delete the replies that are related to that topic too. So I made that with the following code

$topic = Topic::find($id);

$topic->replies()->delete();

$topic->delete();

return redirect('/');

But now I'm trying to delete a theme. Which has to delete every topic that is related to that theme and every reply that is related to every topic within that theme. But how do I do this? I figured it may be something like this?

$theme = Theme::find($id);
$theme->topics()->delete();
$theme->topics()->replies()->delete()
$theme->delete();

return redirect('/');

But that doesn't work. How can I make this work? Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire