public function get()
 {
     $launchChanges = PrelaunchEvent::where('event', 'Launch Change')->whereHas('mission', function ($q) {
         $q->where('status', 'Complete');
     })->orderBy('mission_id')->orderBy('prelaunch_event_id')->get();
     $countOfCompletedMissions = Mission::where('status', 'Complete')->get();
     print_r($launchChanges);
 }
 public function run()
 {
     // Grab all missions as an array, pass them through
     if (is_null($this->mission->mission_id)) {
         $allMissions = Mission::orderBy('launch_order_id', 'ASC')->get();
     } else {
         $allMissions = Mission::where('mission_id', '!=', $this->mission->mission_id)->orderBy('launch_order_id', 'ASC')->get();
     }
     $arrayedMissions = $allMissions->toArray();
     // Add the mission we are running from to the array
     array_push($arrayedMissions, array('context' => true, 'launch_date_time' => $this->scheduledLaunch));
     // Sort the missions by launchDateTime
     // Use the at symbol because http://stackoverflow.com/a/10985500/1064923
     @usort($arrayedMissions, function ($a, $b) {
         $ldta = LaunchDateTimeResolver::parseString($a['launch_date_time']);
         $ldtb = LaunchDateTimeResolver::parseString($b['launch_date_time']);
         return LaunchDateTime::compare($ldta, $ldtb);
     });
     // Update each mission's launch_order_id
     foreach ($arrayedMissions as $index => $arrayedMission) {
         // If the context is from the current mission, set the current mission properties
         if (array_key_exists('context', $arrayedMission)) {
             $this->setMissionProperties($index);
             $this->mission->save();
             // Else, update the mission properties if it needs updating
         } else {
             // Check to see if it actually needs updating in the db
             if ($arrayedMission['launch_order_id'] !== $index + 1) {
                 $missionModel = $allMissions->first(function ($key, $value) use($arrayedMission) {
                     return $value->launch_order_id == $arrayedMission['launch_order_id'];
                 });
                 $missionModel->launch_order_id = $index + 1;
                 $missionModel->save();
             }
         }
     }
 }
 public function getSuccessfulConsecutiveLaunchAttribute()
 {
     if ($this->status == MissionStatus::Complete && $this->outcome != MissionOutcome::Failure) {
         try {
             $lastFailedMissionLaunchOrderId = Mission::where('outcome', MissionOutcome::Failure)->before($this->launch_order_id)->firstOrFail()->launch_order_id;
         } catch (ModelNotFoundException $e) {
             $lastFailedMissionLaunchOrderId = 0;
         }
         return $this->launch_order_id - $lastFailedMissionLaunchOrderId;
     }
     return null;
 }