Skip to content

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

@aplauche

Description

@aplauche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions