groupBy('vehicles.id') ->selectRaw('*, sum(vehicle_scores.overall_score) as totalscore') ->whereNotIn('vehicle_scores.vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->orderBy('totalscore','desc') ->first(); return $mainWinnerQuery->vehicle; } function categoryWinner($category) { $categoryWinnerQuery = 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") //->where('vehicles.type', '=', "$category") ->orderBy('totalscore', 'desc') ->first(); return $categoryWinnerQuery->vehicle; } function pcWinner() { $peoplesChoiceWinnerQuery = DB::table('peoples_choice') ->groupBy('peoples_choice.vehicle') ->selectRaw('*, sum(pc_count as totalscore') ->whereNotIn('vehicle', function($query){ $query->select('vehicle')->from('car_show_winners'); }) ->orderBy('totalscore','desc') ->first(); return $peoplesChoiceWinnerQuery->vehicle; } function oughtsTo49Winner() { $oughtsTo49Query = 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', '>=', 1900) ->where('vehicles.year', '<=', 1949) ->orderBy('totalscore','desc') ->first(); return $oughtsTo49Query->vehicle; } function fiftiesWinner() { $fiftiesQuery = 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', '>=', 1950) ->where('vehicles.year', '<=', 1959) ->orderBy('totalscore','desc') ->first(); return $fiftiesQuery->vehicle; } function sixtiesWinner() { $sixtiesQuery = 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', '>=', 1960) ->where('vehicles.year', '<=', 1969) ->orderBy('totalscore','desc') ->first(); return $sixtiesQuery->vehicle; } function seventiesWinner() { $seventiesQuery = 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', '>=', 1970) ->where('vehicles.year', '<=', 1979) ->orderBy('totalscore','desc') ->first(); return $seventiesQuery->vehicle; } function eightiesWinner() { $eightiesQuery = 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', '>=', 1980) ->where('vehicles.year', '<=', 1989) ->orderBy('totalscore','desc') ->first(); return $eightiesQuery->vehicle; } function ninetiesWinner() { $ninetiesQuery = 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', '>=', 1990) ->where('vehicles.year', '<=', 1999) ->orderBy('totalscore','desc') ->first(); return $ninetiesQuery->vehicle; } function thousandsWinner() { $thousandsQuery = 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', '>=', 2000) //->where('vehicles.year', '<=', 1959) ->orderBy('totalscore','desc') ->first(); return $thousandsQuery->vehicle; } //categories /* Here for reference only $individuallyProcessedAwards = array( 'inShowFirst' =>'6', 'pcFirst' => '3' ); */ $mainAwardsToCalculate = array( 'inShowFirst' =>'6', 'pcFirst' => '3', 'bestTruckFirst' => '5', 'corvetteFirst' => '7', 'camaroFirst' => '8', 'mustangFirst' => '1', 'ratRideFirst' => '4' ); $yearAwardsToCalculate = array( 'oughtsTo49First' => '9', 'oughtsTo49Second' => '9', 'fiftiesFirst' => '10', 'fiftiesSecond' => '10', 'sixtiesFirst' => '11', 'sixtiesSecond' => '11', 'seventiesFirst' => '12', 'seventiesSecond' => '12', 'eightiesFirst' => '13', 'eightiesSecond' => '13', 'ninetiesFirst' => '14', 'ninetiesSecond' => '14', 'thousandsFirst' => '15', 'thousandsSecond' => '15' ); //Insert Best In Show Winner CarShowWinner::updateOrCreate( [ 'category' => '6', 'place' => 'first' ], [ 'vehicle' => mainWinner() ] ); //Insert People's Choice Winner /*CarShowWinner::updateOrCreate( [ 'category' => '3', 'place' => 'first' ], [ 'vehicle' => $peoplesChoiceWinnerQuery ] );*/ //Insert Category Award Winners foreach($mainAwardsToCalculate as $mainaward => $category) { $categorywinner = CarShowWinner::updateOrCreate( [ 'category' => $category, 'place' => 'first' ], [ 'vehicle' => categoryWinner($category) ] ); } //Insert Year Award Winners //1900-1949 CarShowWinner::updateOrCreate( [ 'category' => '9', 'place' => 'first' ], [ 'vehicle' => oughtsTo49Winner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '9', 'place' => 'second' ], [ 'vehicle' => oughtsTo49Winner() ] ); //1950-1959 CarShowWinner::updateOrCreate( [ 'category' => '10', 'place' => 'first' ], [ 'vehicle' => fiftiesWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '10', 'place' => 'second' ], [ 'vehicle' => fiftiesWinner() ] ); //1960-1969 CarShowWinner::updateOrCreate( [ 'category' => '11', 'place' => 'first' ], [ 'vehicle' => sixtiesWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '11', 'place' => 'second' ], [ 'vehicle' => sixtiesWinner() ] ); //1970-1979 CarShowWinner::updateOrCreate( [ 'category' => '12', 'place' => 'first' ], [ 'vehicle' => seventiesWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '12', 'place' => 'second' ], [ 'vehicle' => seventiesWinner() ] ); //1980-1989 CarShowWinner::updateOrCreate( [ 'category' => '13', 'place' => 'first' ], [ 'vehicle' => eightiesWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '13', 'place' => 'second' ], [ 'vehicle' => eightiesWinner() ] ); //1990-1999 CarShowWinner::updateOrCreate( [ 'category' => '14', 'place' => 'first' ], [ 'vehicle' => ninetiesWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '14', 'place' => 'second' ], [ 'vehicle' => ninetiesWinner() ] ); //2000-present CarShowWinner::updateOrCreate( [ 'category' => '15', 'place' => 'first' ], [ 'vehicle' => thousandsWinner() ] ); CarShowWinner::updateOrCreate( [ 'category' => '15', 'place' => 'second' ], [ 'vehicle' => thousandsWinner() ] ); } }