public static function engines($substatistic, $dynamicString)
 {
     if ($substatistic == 'Flown') {
         $partFlights = Cache::remember('stats:description:engines:flown', 60, function () {
             return PartFlight::whereIn('firststage_engine', ['Merlin 1D', 'Merlin 1D Fullthrust'])->whereHas('mission', function ($q) {
                 return $q->whereComplete();
             })->count();
         });
         if ($dynamicString === 'engineCount') {
             return $partFlights * 9;
         }
         if ($dynamicString === 'flightCount') {
             return $partFlights;
         }
     }
 }
 private function managePartFlightRelations()
 {
     $currentPartFlights = $this->mission->partFlights->keyBy('part_flight_id');
     foreach ($this->input['mission']['part_flights'] as $partFlightInput) {
         // If the partFlight exists, update it, otherwise, create it
         if (array_key_exists('part_flight_id', $partFlightInput)) {
             $partFlight = $currentPartFlights->pull($partFlightInput['part_flight_id']);
         } else {
             $partFlight = new PartFlight();
         }
         // Create part if it is not being reused or otherwise find it
         $partInput = array_pull($partFlightInput, 'part');
         $part = array_key_exists('part_id', $partInput) ? Part::find($partInput['part_id']) : new Part();
         $part->fill($partInput);
         $part->save();
         $partFlight->part()->associate($part);
         $partFlight->mission()->associate($this->mission);
         $partFlight->save();
     }
     // Delete any remaining partflights
     if (!$currentPartFlights->isEmpty()) {
         $partFlightsToDelete = PartFlight::whereIn('part_flight_id', $currentPartFlights->keys());
         // For each partFlight, if it has a part which has not been used elsewhere, also delete it
         foreach ($partFlightsToDelete as $partFlightToDelete) {
             if ($partFlight->part()->partFlights()->count() > 1) {
                 $partFlightToDelete->part()->delete();
             }
             $partFlightToDelete->delete();
         }
     }
 }
 /**
  * Fetch the number of firststage Merlin 1D engines (both normal and fullthrust) flown on operational missions
  *
  * @param $substatistic
  * @return int
  */
 public static function engines($substatistic)
 {
     if ($substatistic === 'Flown') {
         return Cache::remember('stats:engines:flown', 60, function () {
             return PartFlight::whereIn('firststage_engine', [Engine::Merlin1D, Engine::Merlin1DFullThrust])->whereHas('mission', function ($q) {
                 return $q->whereComplete();
             })->count() * 9;
         });
     }
     if ($substatistic === 'M1D Flight Time') {
         return Cache::remember('stats:engines:M1DFlightTime', 60, function () {
             return PartFlight::select(DB::raw('SUM(part_flights_pivot.firststage_meco) AS flight_time'))->whereHas('mission', function ($q) {
                 $q->whereComplete();
             })->first()->flight_time;
         });
     }
     if ($substatistic === 'M1D Success Rate') {
         return Cache::remember('status:engines:M1DSuccessRate', 60, function () {
             return PartFlight::select(DB::raw('ROUND(100 - (SUM(part_flights_pivot.firststage_engine_failures) / (COUNT(part_flights_pivot.mission_id) * 9) * 100)) AS success_rate'))->whereHas('mission', function ($query) {
                 $query->whereComplete()->whereSpecificVehicle(['Falcon 9 v1.1', 'Falcon 9 v1.2']);
             })->first()->success_rate;
         });
     }
 }