app\Topic::votes PHP Method

votes() public method

public votes ( )
    public function votes()
    {
        return $this->hasMany(Vote::class);
    }

Usage Example

コード例 #1
0
ファイル: Voter.php プロジェクト: yhbyun/l5-forum
 public function topicDownVote(Topic $topic)
 {
     if ($topic->votes()->ByWhom(auth()->id())->WithType('downvote')->count()) {
         // click second time for remove downvote
         $topic->votes()->ByWhom(auth()->id())->WithType('downvote')->delete();
         $topic->increment('vote_count', 1);
     } elseif ($topic->votes()->ByWhom(auth()->id())->WithType('upvote')->count()) {
         // user already clicked upvote once
         $topic->votes()->ByWhom(auth()->id())->WithType('upvote')->delete();
         $topic->votes()->create(['user_id' => auth()->id(), 'is' => 'downvote']);
         $topic->decrement('vote_count', 2);
     } else {
         // click first time
         $topic->votes()->create(['user_id' => auth()->id(), 'is' => 'downvote']);
         $topic->decrement('vote_count', 1);
     }
 }