/** * Run the database seeds. * * @return void */ public function run() { $csv = Reader::createFromPath(storage_path('app/csv/allied_awards.csv')); // This will read the CSV as set of data into the listed array keys $awardsData = $csv->fetchAssoc(['awardType', 'isAllied', 'key', 'label', 'description', 'awardedTo', 'awardedBy', 'frequency', 'pri', 'awardReason', 'stat', 'country', 'branch', 'imageUrl'], function ($row) { // /** * manipulate the incoming data as it is read. This is turn it into * a set of data which can be immediately added to our awards table * * Note that the CSV reader is REALLY weird. The order in which you do * things in this function actually matters in the returned array. */ $drow = []; $key = snake_case(strtolower(trim($row[1]))); switch ($key) { case 'aerialgunnerwings': case 'chiefgunnersmatebadge': case 'combatriflemanbadge': case 'expertmarksmanbadge': case 'fafacewingsbomber': case 'fafacewingsfighter': case 'fafnavalaviatorwings': case 'fafpilotwings': case 'fieldgunnerybadge': case 'flakscreenbadge': case 'guncrewbadge': case 'gunnersmatebadge': case 'paratroopwings': case 'rafbomberacewings': case 'rafnavalaviatorwings': case 'rafpilotwings': case 'seawolfbadge': case 'sharpshooterbadge': case 'tailgunnergoldwings': case 'tankdestroyerbadge': case 'tankhunterbadge': case 'transportpilotwings': $drow['awardType'] = 'badge'; break; case 'elitecommandoaward': $drow['awardType'] = 'medal'; break; default: $drow['awardType'] = 'ribbon'; } $drow['isAllied'] = true; $drow['key'] = $key; $drow['label'] = trim($row[1]); $drow['description'] = trim($row[3]); $drow['awardedTo'] = strtolower(trim($row[4])); $drow['awardedBy'] = strtolower(trim($row[5])); $drow['frequency'] = strtolower(trim($row[6])); $drow['pri'] = trim($row[7]); $drow['awardReason'] = strtolower(trim($row[8])); $drow['stat'] = strtolower(trim($row[9])) == 'y'; $drow['country'] = strtolower(trim($row[10])); $drow['branch'] = strtolower(trim($row[11])); $drow['imageUrl'] = trim($row[2]); return $drow; }); Award::unguard(); $client = new Client(); foreach ($awardsData as $item) { // Takes the url of the image $path_parts = pathinfo($item['imageUrl']); // download the image and store it locally $local_path = storage_path('app\\img') . '\\' . $item['key'] . '.' . strtolower($path_parts['extension']); if (!file_exists($local_path)) { try { $client->get($item['imageUrl'], ['save_to' => $local_path]); } catch (Exception $ex) { // A problem with getting the image from the remote server Log::error(sprintf('Unable to download %s to %s: %s', $item['imageUrl'], $local_path, $ex->getMessage())); } } // Make sure it doesn't try to the save the image to the database unset($item['imageUrl']); // Save the award to the database Award::create($item); } Award::reguard(); }