Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$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
You can’t perform that action at this time.