コード例 #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // Read the database of allied awards
     $csv = Reader::createFromPath(storage_path('app/csv/issued_allied_awards.csv'));
     // This will read the CSV as set of data into the listed array keys
     $data = $csv->fetchAssoc(['playerName', 'playerLabel', 'awardName', 'campaign_id', 'awardedAt', 'submitterName', 'cincName', 'forumSource'], 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
         $drow = [];
         $drow['playerName'] = trim(strtolower($row[1]));
         $drow['playerLabel'] = trim($row[1]);
         // case as it appears in csv
         $drow['awardName'] = snake_case(strtolower(trim($row[2])));
         $drow['campaign_id'] = trim($row[3]) == '' ? null : trim($row[3]);
         $drow['awardedAt'] = trim($row[6]);
         $drow['submitterName'] = trim(strtolower($row[7]));
         $drow['cincName'] = trim(strtolower($row[8]));
         $drow['forumSource'] = trim(strtolower($row[9]));
         return $drow;
     });
     Player::unguard();
     foreach ($data as $item) {
         $award = Award::where('key', $item['awardName'])->first();
         if ($award == null) {
             Log::info(sprintf('Could not find an award with key %s', $item['awardName']));
             continue;
         }
         $submitter = Player::where('playerName', $item['submitterName'])->first();
         if ($submitter == null) {
             $submitter = Player::create(['playerName' => $item['submitterName'], 'isActive' => true]);
         }
         $cinc = Player::where('playerName', $item['cincName'])->first();
         if ($cinc == null) {
             $cinc = Player::create(['playerName' => $item['cincName'], 'isActive' => true]);
         }
         // check the rype of award we are presenting. If it's a squad award, find the squad and apply it
         // also, if there is a space in the name, its not a player name
         if ($award->awardedTo == 'squad' || preg_match('/\\s/', $item['playerName']) > 0) {
             $squad = Squad::where('key', snake_case($item['playerName']))->first();
             if ($squad == null) {
                 $squad = Squad::create(['key' => snake_case($item['playerName']), 'label' => $item['playerLabel'], 'isActive' => true]);
             }
             $squad->awards()->save($award, ['awardedBy_id' => $submitter->id, 'cinc_id' => $cinc->id, 'campaign_id' => $item['campaign_id'], 'awardedAt' => Carbon\Carbon::createFromFormat('d/m/Y', $item['awardedAt']), 'forumLink' => $item['forumSource']]);
         } else {
             $player = Player::where('playerName', $item['playerName'])->first();
             if ($player == null) {
                 $player = Player::create(['playerName' => $item['playerName'], 'isActive' => true]);
             }
             $player->awards()->save($award, ['awardedBy_id' => $submitter->id, 'cinc_id' => $cinc->id, 'campaign_id' => $item['campaign_id'], 'awardedAt' => Carbon\Carbon::createFromFormat('d/m/Y', $item['awardedAt']), 'forumLink' => $item['forumSource']]);
         }
     }
     Player::reguard();
 }