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