public function updateAllWeightedAverage()
 {
     $all = WeightedAverage::where("ratingable_type", "Iterinary")->get();
     foreach ($all as $wa) {
         //check if number of votes reaches 5 (minimum)
         if ($this->checkMinVotes($wa->ratingable_id)) {
             // dd("in");
             $numOfVotes = count(Rating::where("ratingable_type", "Iterinary")->where("ratingable_id", $wa->ratingable_id)->get());
             $avgRate = Rating::where("ratingable_type", "Iterinary")->where("ratingable_id", $wa->ratingable_id)->selectRaw("avg(value)")->get();
             $avgAll = Rating::where("ratingable_type", "Iterinary")->selectRaw("avg(value)")->get();
             foreach ($avgRate as $value) {
                 $avgRate = $value['avg(value)'];
             }
             foreach ($avgAll as $value) {
                 $avgAll = $value['avg(value)'];
             }
             //calculate weighted average (base Bayesian rating W = Rv+Cm/v+m )
             $weighted_average = round(($avgRate * $numOfVotes + $avgAll * 5) / ($numOfVotes + 5), 1);
             // dd($avgRate,$numOfVotes, $avgAll, $weighted_average);
             // dd($wa->id);
             $update_weighted_average = WeightedAverage::find($wa->id);
             $update_weighted_average->average = $weighted_average;
             $update_weighted_average->save();
             // return "updated successfully";
         } else {
             // dd("cannot get WeightedAverage");
         }
     }
 }
 public function getRecommendation(Request $request)
 {
     $origin = $request->origin;
     $destination = $request->destination;
     $userbudget = $request->budget;
     // dd($origin,$destination);
     // $origin = "cebu city";
     // $destination = "manila";
     //get closest budget
     $budget = $this->getBudgetRecommendation($userbudget, $origin, $destination);
     if ($budget == 0) {
         // dd("in");
         $segments = $this->get_recommend($origin, $destination);
         // dd($segments);
         return response()->json($segments);
     } else {
         //get iterinaries that falls under the budget
         $iterinary_choices = Iterinary::whereRaw("price <= ?", [$budget])->where("origin", $origin)->where("destination", $destination)->lists("id");
         // dd($iterinary_choices);
         //get the iterinary_id that falls under the budget and is the best in terms of rating
         $suggested_iterinary = WeightedAverage::whereIn("ratingable_id", $iterinary_choices)->where("ratingable_type", "Iterinary")->max("average");
         $suggested_iterinary = WeightedAverage::where("average", $suggested_iterinary)->where("ratingable_type", "Iterinary")->lists("ratingable_id");
         //refers to points if there are iterinaries with the same rate
         // dd($suggested_iterinary);
         if (count($suggested_iterinary) > 1) {
             $suggested_iterinary = $this->getSuggestedIterinary($suggested_iterinary);
         }
         // dd($suggested_iterinary);
         $route_id = Iterinary::where("id", $suggested_iterinary)->lists("route_id");
         $segments = Segment::where("route_id", $route_id)->get();
         return response()->json($segments);
         // dd($segments);
         // dd($route_id);
         // dd($iterinary_choices);
     }
 }