Laravel increment

I'm having problem with increment. I have query in model.

public function scopeViewCount($query)
{
    return $query->increment('view', 1);
}

My controller.

public function quoteBySlug($slug)
{
    $quote = Article::bySlug($slug);
            
    if (! $quote) {
        abort(404);
    }
            
    $quote->viewCount();
            
    return view('pages/quote/quote-detailed')->with('quote', $quote);
}

Its working but problem is that its adding +2 some times even +4 but i need only +1 so what's i'm doing wrong?

Upvotes: 0

Views: 136

Answers (1)

Maksim
Maksim

Reputation: 2957

You using scopes wrong. Scopes is for define common sets of query constraints, see docs.

Than, just remove scope prefix:

public function incrementViewCount()
    {
        return $this->increment('view', 1);
    }

$quote->incrementViewCount();

Upvotes: 2

Related Questions