groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $bestInShowWinnerQuery->vehicle; } function bestInShow2ndWinner() { $bestInShow2ndWinnerQuery = VehicleScores::join('vehicles', 'vehicle_scores.vehicle', '=', 'vehicles.id') ->groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $bestInShow2ndWinnerQuery->vehicle; } function pcWinner() { $peoplesChoiceWinnerQuery = PeoplesChoice::join('vehicles', 'peoples_choice.vehicle', '=', 'vehicles.id') ->groupBy('peoples_choice.vehicle') ->selectRaw('*, sum(pc_count) as totalscore') ->whereNotIn('vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $peoplesChoiceWinnerQuery->vehicle; } function pc2ndWinner() { $peoplesChoice2ndWinnerQuery = PeoplesChoice::join('vehicles', 'peoples_choice.vehicle', '=', 'vehicles.id') ->groupBy('peoples_choice.vehicle') ->selectRaw('*, sum(pc_count) as totalscore') ->whereNotIn('vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $peoplesChoice2ndWinnerQuery->vehicle; } function zeroTo42Winner() { $zeroTo43Query = VehicleScores::join('vehicles', 'vehicle_scores.vehicle', '=', 'vehicles.id') ->groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.year', '>=', 0) ->where('vehicles.year', '<=', 1942) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $zeroTo43Query->vehicle; } function fortyThreeToSixtyEightWinner() { $fortyThreeToSixtyEightQuery = VehicleScores::join('vehicles', 'vehicle_scores.vehicle', '=', 'vehicles.id') ->groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.year', '>=', 1943) ->where('vehicles.year', '<=', 1968) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $fortyThreeToSixtyEightQuery->vehicle; } function sixtyEightToNinetyEightWinner() { $sixtyEightToNinetyEightQuery = VehicleScores::join('vehicles', 'vehicle_scores.vehicle', '=', 'vehicles.id') ->groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.year', '>=', 1969) ->where('vehicles.year', '<=', 1998) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $sixtyEightToNinetyEightQuery->vehicle; } function NinetyNineToCurrentWinner() { $NinetyNineToCurrentQuery = VehicleScores::join('vehicles', 'vehicle_scores.vehicle', '=', 'vehicles.id') ->groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->where('vehicles.year', '>=', 1999) //->where('vehicles.year', '<=', 1997) ->where('vehicles.doNotJudge', '=', 0) ->orderBy('totalscore','desc') ->first(); return $NinetyNineToCurrentQuery->vehicle; } // Truncate table first CarShowWinner::truncate(); //Insert Best In Show Winner CarShowWinner::updateOrCreate( [ 'category' => '6', 'place' => 'first' ], [ 'vehicle' => bestInShowWinner() ] ); //Insert People's Choice Winner CarShowWinner::updateOrCreate( [ 'category' => '3', 'place' => 'first' ], [ 'vehicle' => pcWinner() ] ); //Insert Year Award Winners //0-1942 CarShowWinner::updateOrCreate( [ 'category' => '17', 'place' => 'first' ], [ 'vehicle' => zeroTo42Winner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '17', 'place' => 'second' ], [ 'vehicle' => zeroTo42Winner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '17', 'place' => 'third' ], [ 'vehicle' => zeroTo42Winner() ] ); //1943-1967 CarShowWinner::updateOrCreate( [ 'category' => '18', 'place' => 'first' ], [ 'vehicle' => fortyThreeToSixtyEightWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '18', 'place' => 'second' ], [ 'vehicle' => fortyThreeToSixtyEightWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '18', 'place' => 'third' ], [ 'vehicle' => fortyThreeToSixtyEightWinner() ] ); //1968-1997 CarShowWinner::updateOrCreate( [ 'category' => '20', 'place' => 'first' ], [ 'vehicle' => sixtyEightToNinetyEightWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '20', 'place' => 'second' ], [ 'vehicle' => sixtyEightToNinetyEightWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '20', 'place' => 'third' ], [ 'vehicle' => sixtyEightToNinetyEightWinner() ] ); //1998-Current CarShowWinner::updateOrCreate( [ 'category' => '19', 'place' => 'first' ], [ 'vehicle' => NinetyNineToCurrentWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '19', 'place' => 'second' ], [ 'vehicle' => NinetyNineToCurrentWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '19', 'place' => 'third' ], [ 'vehicle' => NinetyNineToCurrentWinner() ] ); } }