File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,9 @@ class Post extends Model
36
36
37
37
protected $dates = ['deleted_at'];
38
38
39
+ protected $fetchMethod = 'get'; // get, cursor, lazy or chunk
40
+ // protected $chunkSize = 500;
41
+
39
42
public function comments()
40
43
{
41
44
return $this->hasMany(Comment::class);
Original file line number Diff line number Diff line change @@ -65,8 +65,23 @@ protected function cascadeSoftDeletes($relationship)
65
65
{
66
66
$ delete = $ this ->forceDeleting ? 'forceDelete ' : 'delete ' ;
67
67
68
- foreach ( $ this ->{ $ relationship }()-> get () as $ model ) {
68
+ $ cb = function ( $ model ) use ( $ delete ) {
69
69
isset ($ model ->pivot ) ? $ model ->pivot ->{$ delete }() : $ model ->{$ delete }();
70
+ };
71
+
72
+ $ this ->handleRecords ($ relationship , $ cb );
73
+ }
74
+
75
+ private function handleRecords ($ relationship , $ cb )
76
+ {
77
+ $ fetchMethod = $ this ->fetchMethod ?? 'get ' ;
78
+
79
+ if ($ fetchMethod == 'chunk ' ) {
80
+ $ this ->{$ relationship }()->chunk ($ this ->chunkSize ?? 500 , $ cb );
81
+ } else {
82
+ foreach ($ this ->{$ relationship }()->$ fetchMethod () as $ model ) {
83
+ $ cb ($ model );
84
+ }
70
85
}
71
86
}
72
87
You can’t perform that action at this time.
0 commit comments