$data['symbol'], 'name' => $data['name'], 'currency' => $data['currency'], 'meta_data' => json_encode($meta_data), ]; $rowCount++; if ($rowCount % $chunkSize == 0) { $this->bulkInsert($rows); $rows = []; } } } // final clean up if (! empty($rows)) { $this->bulkInsert($rows); $rows = []; } // Close the CSV file fclose($handle); echo "\n > Imported $rowCount market data items successfully!"; } else { echo "Failed to open the CSV.\n"; } } private function bulkInsert($rows): void { try { DB::table('market_data')->upsert($rows, ['symbol'], ['name', 'currency', 'meta_data']); } catch (\Throwable $e) { throw new \Exception('Error: '.$e->getMessage()); } gc_collect_cycles(); } }