Home > database >  Laravel increment
Laravel increment

Time:10-12

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?

CodePudding user response:

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();
  • Related