diff --git a/README.md b/README.md index ed7a93a..14df9bf 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ class Post extends Model protected $dates = ['deleted_at']; + protected $fetchMethod = 'get'; // get, cursor, lazy or chunk + // protected $chunkSize = 500; + public function comments() { return $this->hasMany(Comment::class); diff --git a/src/CascadeSoftDeletes.php b/src/CascadeSoftDeletes.php index 72d57f3..8193bff 100644 --- a/src/CascadeSoftDeletes.php +++ b/src/CascadeSoftDeletes.php @@ -65,8 +65,23 @@ protected function cascadeSoftDeletes($relationship) { $delete = $this->forceDeleting ? 'forceDelete' : 'delete'; - foreach ($this->{$relationship}()->get() as $model) { + $cb = function($model) use ($delete) { isset($model->pivot) ? $model->pivot->{$delete}() : $model->{$delete}(); + }; + + $this->handleRecords($relationship, $cb); + } + + private function handleRecords($relationship, $cb) + { + $fetchMethod = $this->fetchMethod ?? 'get'; + + if ($fetchMethod == 'chunk') { + $this->{$relationship}()->chunk($this->chunkSize ?? 500, $cb); + } else { + foreach($this->{$relationship}()->$fetchMethod() as $model) { + $cb($model); + } } }