App\Models\Forum\PollOption::updateTotals PHP Method

updateTotals() public static method

public static updateTotals ( $filters )
    public static function updateTotals($filters)
    {
        $staticTable = (new static())->table;
        $countQuery = PollVote::where(['topic_id' => DB::raw($staticTable . '.topic_id'), 'poll_option_id' => DB::raw($staticTable . '.poll_option_id')])->select(DB::raw('COUNT(DISTINCT vote_user_id)'))->toSql();
        return static::where($filters)->update(['poll_option_total' => DB::raw("({$countQuery})")]);
    }

Usage Example

Example #1
0
 public function save()
 {
     if (!$this->isValid()) {
         return false;
     }
     return DB::transaction(function () {
         $this->topic->update(['poll_last_vote' => Carbon::now()]);
         $this->topic->pollVotes()->where('vote_user_id', $this->params['user_id'])->delete();
         foreach (array_unique($this->params['option_ids']) as $optionId) {
             $this->topic->pollVotes()->create(['poll_option_id' => $optionId, 'vote_user_id' => $this->params['user_id'], 'vote_user_ip' => $this->params['ip']]);
         }
         PollOption::updateTotals(['topic_id' => $this->topic->topic_id]);
         return true;
     });
 }