0

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?

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

It was adding +2 or +4 because of query?
Its because laravel magic - laravel can call scope much times behind the scene.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.