File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed
Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change 1919use App \Models \User ;
2020use Illuminate \Database \Eloquent \Builder ;
2121use Illuminate \Support \Facades \Auth ;
22+ use Illuminate \Support \Facades \DB ;
2223use Illuminate \Support \Str ;
2324
2425class OrderService
@@ -126,14 +127,19 @@ public function getAll(): array
126127 /**
127128 * Clear old orders that are older than 2 weeks, have no items, and have no user_id.
128129 *
129- * @return int Number of orders deleted
130+ * @return void Number of orders deleted
130131 */
131- public function clearOldOrders (): int
132+ public function clearOldOrders (): void
132133 {
133- // Delete all the order items first to avoid foreign key constraint issues
134- OrderItem::whereIn ('order_id ' , $ this ->getQueryOldOrders ()->select ('id ' ))->delete ();
134+ DB ::transaction (function (): void {
135+ // Delete all the order items first to avoid foreign key constraint issues
136+ $ chunk = $ this ->getQueryOldOrders ()->pluck ('id ' )->chunk (100 );
137+ foreach ($ chunk as $ old_orders_ids ) {
138+ OrderItem::whereIn ('order_id ' , $ old_orders_ids )->delete ();
139+ }
140+ $ this ->getQueryOldOrders ()->delete ();
141+ });
135142
136- return $ this ->getQueryOldOrders ()->delete ();
137143 }
138144
139145 /**
You can’t perform that action at this time.
0 commit comments