1 year ago

Save votes of a model in database

Posted 1 year ago by SmokeTM

I wanna save my article votes in to database, but I am not able to do so. Here is my code which doesn't work properly.

    public function store(Article $article, Request $request)

        if(Gate::denies('vote', $article)) {
          abort(403, 'This action is unauthorized.');

        $user = auth()->user();

        $voted = $user->votedFor($article);

        if($voted) {
        } else {

        /* Update votes */
        $article->article_votes = $article->votes->count();
        /* Set article to active if there are enough votes.
        *   Means Article will be displayed on the front page.

        return response()->json(['success' => true, 'count' => $article->fresh()->votes->count(), 'voted' => $voted]);

Please sign in or create an account to participate in this conversation.