public static function bootCascadesDeletes()
    {
        // Setup the 'deleting' event listener.
        static::deleting(function ($model) {

            // Wrap all of the cascading deletes inside of a transaction to make this an
            // all or nothing operation. Any exceptions thrown inside the transaction
            // need to bubble up to make sure all transactions will be rolled back.
            $model->getConnectionResolver()->transaction(function () use ($model) {

                $relations = $model->getCascadeDeletesRelations();

                if ($invalidRelations = $model->getInvalidCascadeDeletesRelations($relations)) {
                    throw new LogicException(sprintf('[%s]: invalid relationship(s) for cascading deletes. Relationship method(s) [%s] must return an object of type Illuminate\Database\Eloquent\Relations\Relation.', static::class, implode(', ', $invalidRelations)));
                }

                $deleteMethod = $model->isCascadeDeletesForceDeleting() ? 'forceDelete' : 'delete';

                foreach ($relations as $relationName => $relation) {
                    $expected = 0;
                    $deleted = 0;

                    if ($relation instanceof BelongsToMany) {
                        // Process the many-to-many relationships on the model.
                        // These relationships should not delete the related
                        // record, but should just detach from each other.

                        $expected = $model->getCascadeDeletesRelationQuery($relationName)->count();

                        $deleted = $model->getCascadeDeletesRelationQuery($relationName)->detach();
                    } elseif ($relation instanceof HasOneOrMany) {
                        // Process the one-to-one and one-to-many relationships
                        // on the model. These relationships should actually
                        // delete the related records from the database.

                        $children = $model->getCascadeDeletesRelationQuery($relationName)->get();

                        // To protect against potential relationship defaults,
                        // filter out any children that may not actually be
                        // Model instances, or that don't actually exist.
                        $children = $children->filter(function ($child) {
                            return $child instanceof Model && $child->exists;
                        })->all();

                        $expected = count($children);

                        foreach ($children as $child) {
                            // Delete the record using the proper method.
                            $child->$deleteMethod();

                            // forceDelete doesn't return anything until Laravel 5.2. Check
                            // exists property to determine if the delete was successful
                            // since that is the last thing set before delete returns.
                            $deleted += !$child->exists;
                        }
                    } else {
                        // Not all relationship types make sense for cascading. As an
                        // example, for a BelongsTo relationship, it does not make
                        // sense to delete the parent when the child is deleted.
                        throw new LogicException(sprintf('[%s]: error occurred deleting [%s]. Relation type [%s] not handled.', static::class, $relationName, get_class($relation)));
                    }

                    if ($deleted < $expected) {
                        throw new LogicException(sprintf('[%s]: error occurred deleting [%s]. Only deleted [%d] out of [%d] records.', static::class, $relationName, $deleted, $expected));
                    }
                }
            });
        });
    }