Guest User

Untitled

a guest
Nov 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // get your locations
  2. $locations = ['Berlin', 'Hamburg', 'Stuttgart', 'Hannover', 'Heidelberg', 'Bern' ...];
  3.  
  4. // make them into collection if not already
  5. $locations_coll = Collection::make($locations);
  6.  
  7. // chunk the database insert if there are too many records in the collection
  8. $locations_coll->chunk(500)->each(function($chunk) {
  9.  
  10. $inserts = '';
  11. // loop through locations
  12. foreach($chunk as $location1) {
  13. foreach($chunk as $location2) {
  14.  
  15. // if locations are the same then skip
  16. if($location1==$location2) continue;
  17.  
  18. // add to bulk inserts
  19. $inserts .= '(' . $location1 . '-' . $location2 . '), ' . PHP_EOL;
  20. }
  21. }
  22.  
  23. // do the bulk insert
  24. try {
  25. DB::insert("INSERT INTO your_table (`locations`) VALUES $inserts");
  26. } catch (Exception $e) {
  27. print_r([$e->getMessage()]);
  28. }
  29. });
Add Comment
Please, Sign In to add comment