Skip to content

Instantly share code, notes, and snippets.

@troyxmccall
Forked from niallobrien/cascading-deletes.php
Last active August 29, 2015 14:08
Show Gist options
  • Save troyxmccall/dea1a692f9b57d5f52d3 to your computer and use it in GitHub Desktop.
Save troyxmccall/dea1a692f9b57d5f52d3 to your computer and use it in GitHub Desktop.
<?php
// I have Groups. A group can have many discussions. A single discussion can have many posts.
// models/Group.php
public function delete()
{
// Check for discussions belonging to the group first
if ($this->discussions) {
foreach ($this->discussions as $discussion) {
$discussion->delete();
}
}
// Remove entry in group_user pivot table
$this->users()->delete();
// Now delete the group
return parent::delete();
}
// models/Discussion.php
function delete()
{
// Check for posts to discussion first
if ($this->posts) {
foreach ($this->posts as $post) {
$post->delete();
}
}
// Now delete the discussion
return parent::delete();
}
// The Post model does pretty much the same as the Discussion model.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment