Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Book reviews project needs to pass from and to variable down to withAvgRating / withReviewsCount scopes #4

Open
aplauche opened this issue Oct 3, 2023 · 0 comments

Comments

@aplauche
Copy link

aplauche commented Oct 3, 2023

Hi,

Thanks for some great Laravel practice projects! One quick bug I noticed is in the book review project when we add the withAvgRating and withReviewsCount scope, the previous Popular and HighestRated scopes are not passing along the $from and $to date values so the project's last month / last 6 months filtering breaks.

Suggested update below passes the $from and $to variables through:

    public function scopeWithReviewsCount(Builder $query, $from = null, $to = null): Builder
    {
        return $query->withCount([
            'reviews' => fn (Builder $q) => $this->dateRangeFilter($q, $from, $to)
        ]);
    }

    public function scopeWithAvgRating(Builder $query, $from = null, $to = null): Builder
    {
        return $query->withAvg([
            'reviews' => fn (Builder $q) => $this->dateRangeFilter($q, $from, $to)
        ], 'rating');
    }

    public function scopePopular(Builder $query, $from = null, $to = null): Builder
    {
        return $query->withReviewsCount($from, $to)->orderBy('reviews_count', 'desc');
    }

    public function scopeHighestRated(Builder $query, $from = null, $to = null): Builder
    {
        return $query->withAvgRating($from, $to)->orderBy('reviews_avg_rating', 'desc');
    }

Let me know if you'd prefer me to submit a PR for this - wasn't sure what you'd prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant