Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // get your locations
- $locations = ['Berlin', 'Hamburg', 'Stuttgart', 'Hannover', 'Heidelberg', 'Bern' ...];
- // make them into collection if not already
- $locations_coll = Collection::make($locations);
- // chunk the database insert if there are too many records in the collection
- $locations_coll->chunk(500)->each(function($chunk) {
- $inserts = '';
- // loop through locations
- foreach($chunk as $location1) {
- foreach($chunk as $location2) {
- // if locations are the same then skip
- if($location1==$location2) continue;
- // add to bulk inserts
- $inserts .= '(' . $location1 . '-' . $location2 . '), ' . PHP_EOL;
- }
- }
- // do the bulk insert
- try {
- DB::insert("INSERT INTO your_table (`locations`) VALUES $inserts");
- } catch (Exception $e) {
- print_r([$e->getMessage()]);
- }
- });
Add Comment
Please, Sign In to add comment