Skip to content

Commit

Permalink
Merge pull request #6 from gordanielyan/gordanielyan-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue authored Aug 11, 2022
2 parents f06ad18 + 0aca3ee commit d1290b6
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Traits/Votable.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,23 @@ public function totalDownvotes()

public function scopeWithTotalVotes(Builder $builder): Builder
{
return $builder->withSum('votes as total_votes', 'votes');
return $builder->withSum(['votes as total_votes' =>
fn ($q) => $q->select(\DB::raw('COALESCE(SUM(votes), 0)'))
], 'votes');
}

public function scopeWithTotalUpvotes(Builder $builder): Builder
{
return $builder->withSum(['votes as total_upvotes' => fn ($q) => $q->where('votes', '>', 0)], 'votes');
return $builder->withSum(['votes as total_upvotes' =>
fn ($q) => $q->where('votes', '>', 0)->select(\DB::raw('COALESCE(SUM(votes), 0)'))
], 'votes');
}

public function scopeWithTotalDownvotes(Builder $builder): Builder
{
return $builder->withSum(['votes as total_downvotes' => fn ($q) => $q->where('votes', '<', 0)], 'votes');
return $builder->withSum(['votes as total_downvotes' =>
fn ($q) => $q->where('votes', '<', 0)->select(\DB::raw('COALESCE(SUM(votes), 0)'))
], 'votes');
}

public function scopeWithVotesAttributes(Builder $builder)
Expand Down

0 comments on commit d1290b6

Please sign in to comment.