Cascading deletes in Laravel 4.
<?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.