/** * Increment count of tag by one. This function will create tag record if it does not exist. * * @param string $tagString */ public static function incrementCount($tagString, $tagSlug, $count) { if ($count <= 0) { return; } $tag = static::where('slug', '=', $tagSlug)->first(); if (!$tag) { $tag = new Tag(); $tag->name = $tagString; $tag->slug = $tagSlug; $tag->suggest = false; $tag->save(); } $tag->count = $tag->count + $count; $tag->save(); }
/** * Removes a single tag * * @param $tagName string */ private function removeTag($tagName) { $tagName = trim($tagName); $tagSlug = Tag::slug($tagName); if ($count = $this->tagged()->where('tag_slug', '=', $tagSlug)->delete()) { Tag::decrementCount($tagName, $tagSlug, $count); } }