Created
October 28, 2018 16:50
-
-
Save Tjoosten/2c7f3d8e17e10bde261b62b9163e684a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $csv = Reader::createFromPath(database_path('seeds/sources/belgian-cities.csv'), 'r'); | |
| $csv->setHeaderOffset(0); | |
| foreach ($stmt->process($csv) as $city) { | |
| // Create a new province if not exists | |
| $province = $provinces->firstOrCreate(['name' => $city['province']]); | |
| // Create a new city in the database if not exists | |
| $cityInformation = $cities->firstOrCreate([ | |
| // City information | |
| 'province_id' => $province->id, 'postal' => $city['postal'], 'name' => $city['name'], 'lat' => $city['lat'], 'lng' => $city['lng'] | |
| ])->setStatus('pending');; | |
| // Attach province to the create city | |
| $cityInformation->province()->associate($province)->save(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment