rows[] = [ 'symbol' => $data['symbol'], 'name' => $data['name'], 'meta_data' => json_encode([ 'country' => $data['country'], 'first_trade_year' => $data['first_trade_year'], 'sector' => $data['sector'], 'industry' => $data['industry'], ]), ]; $rowCount++; if ($rowCount % $chunkSize == 0) { $this->bulkInsert($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()); } } }