-
app/Observers/TaskObserver.php
Open in GitHub// 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(); } } }
-
app/Providers/AppServiceProvider.php
Open in GitHub// All Observer classes are registered in AppServiceProvider, then they are automatically enabled. class AppServiceProvider extends ServiceProvider { public function boot() { Client::observe(ClientObserver::class); Task::observe(TaskObserver::class); // ... other observers } }