How can I filter recipes based on their average rating?

Posted 1 year ago by Chaps

Hello! I am trying to create a filter, where a user can filter min and max average rating of an recipe, here's my current code.. thank you in advance.

$recipes = Recipe::query();

$recipes->whereHas('ratings', function($q) use ($min, $max) {
                // 1st this works.. but not in average
                // $q->whereBetween('rating', [$min, $max]);

                // 2nd not working
                // $q->whereBetween('AVG(rating) as rating', [$min, $max]);

                // 3rd not working
                // $q->select(\DB::raw('AVG(rating) as rating'))
                //     ->whereBetween('rating', [$min, $max]);

                // 4th not working
                // $q->whereBetween(\DB::raw("'AVG(rating)'"), [$min, $max]);

Please sign in or create an account to participate in this conversation.