app/Observers/TaskObserver.php
// This observer class takes care of soft-deleting, force-deleting, and restoring related records. class TaskObserver{ private $relations; public function __construct() { $this->relations = [ 'comments', 'documents', 'appointments', 'activity', ]; } public function deleted(Task $task) { foreach ($this->relations as $relation) { $task->$relation()->delete(); } } public function restored(Task $task) { foreach ($this->relations as $relation) { $task->$relation()->withTrashed()->restore(); } } public function forceDeleted(Task $task) { foreach ($this->relations as $relation) { $task->$relation()->forceDelete(); } }}