定制 ritechoice23/taggable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ritechoice23/taggable

Composer 安装命令:

composer require ritechoice23/taggable

包简介

The most intuitive and powerful Laravel tagging package that just works. Add intelligent tag normalization, trending analytics, powerful queries, and bulletproof edge case handling with zero configuration required.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

The most intuitive and powerful Laravel tagging package that just works. Add $post->tag('laravel') to any model and get intelligent tag normalization, trending analytics, powerful queries, and bulletproof edge case handling - all with zero configuration required.

Why this package

Popular Alternatives

What Makes It Unique

Trending Score Algorithm - Configurable trending calculation (volume, recency, velocity, freshness weights)

Tag Analytics - Built-in getActivitySummary(), growth rate tracking

Momentum Bonuses - Reward tags with consistent daily/weekly activity

Meta Attributes - Color, description, icon stored as JSON on tags

High Growth Detection - whereHighGrowth() scope for discovering rising topics

Artisan Command - php artisan tags:calculate-trending

Installation

You can install the package via composer:

composer require ritechoice23/laravel-taggable

You can publish and run the migrations with:

php artisan vendor:publish --tag="taggable-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="taggable-config"

The config file allows you to customize the trending score calculation:

return [
    'trending' => [
        'weights' => [
            'volume' => 0.25,
            'recency' => 0.30,
            'velocity' => 0.25,
            'freshness' => 0.20,
        ],
        'time_periods' => [
            'daily' => 1,
            'weekly' => 7,
            'monthly' => 30,
            'velocity_comparison' => 14,
        ],
        'scoring' => [
            'volume_normalization' => 50,
            'freshness_decay' => 2,
            'velocity_multiplier' => 50,
        ],
        'momentum_bonuses' => [
            'daily_activity' => 1.1,
            'weekly_threshold_40' => 1.15,
            'weekly_threshold_60' => 1.2,
        ],
    ],
];

Optionally, you can publish the views using

Usage

Add the Trait to Your Model

use Ritechoice23\Taggable\Traits\HasTags;

class Post extends Model
{
    use HasTags;
}

Tagging Models

The package provides a clean, intuitive API for managing tags using natural method names:

$post = Post::find(1);

// Attach tags (accepts strings, IDs, or Tag models)
$post->tag('laravel');
$post->tag(['php', 'programming']);

// Detach tags
$post->untag('laravel');
$post->untag(['php', 'programming']);

// Detach all tags
$post->untagAll();

// Replace all existing tags (retag)
$post->retag(['laravel', 'php']);

Checking Tags

// Check if model has a specific tag
if ($post->hasTag('laravel')) {
    // ...
}

// Check if model has any of the given tags
if ($post->hasAnyTag(['laravel', 'php'])) {
    // ...
}

// Check if model has all of the given tags
if ($post->hasAllTags(['laravel', 'php'])) {
    // ...
}

Querying Tagged Models

// Get posts with a specific tag
$laravelPosts = Post::withTag('laravel')->get();

// Get posts with any of the given tags
$phpPosts = Post::withAnyTag(['laravel', 'php'])->get();

// Get posts with all of the given tags
$taggedPosts = Post::withAllTags(['laravel', 'php'])->get();

Available Query Scopes

For Tagged Models (using HasTags trait):

  • withTag($tag) - Get models with a specific tag (by name, slug, ID, or Tag model)
  • withAnyTag($tags) - Get models with any of the given tags
  • withAllTags($tags) - Get models with all of the given tags

For Tag Model:

  • wherePopular($limit) - Get most popular tags ordered by usage count
  • whereTrending($limit) - Get trending tags ordered by trending score
  • whereName($name) - Search tags by name (supports partial match)
  • whereSlug($slug) - Find tags by slug (exact match)
  • whereRecentlyActive($days) - Get tags active within the specified number of days
  • whereHighGrowth($days) - Get tags with high growth in the specified period

Working with Tags

use Ritechoice23\Taggable\Models\Tag;

// Create a tag
$tag = Tag::create([
    'name' => 'Laravel',
    'meta' => [
        'color' => '#FF2D20',
        'description' => 'A PHP framework for web artisans',
        'icon' => 'laravel-icon',
    ],
]);

// Or set meta values individually
$tag->setMeta('color', '#FF2D20');
$tag->setMeta('description', 'A PHP framework for web artisans');

// Get meta values
$color = $tag->getMeta('color');
$description = $tag->getMeta('description', 'No description');

// Check if meta key exists
if ($tag->hasMeta('color')) {
    // ...
}

// Remove a meta key
$tag->removeMeta('icon');

// Get popular tags (ordered by usage count)
$popularTags = Tag::wherePopular(10)->get();

// Get trending tags (ordered by trending score)
$trendingTags = Tag::whereTrending(10)->get();

// Search tags by name
$searchResults = Tag::whereName('laravel')->get();

// Find tags by slug
$tag = Tag::whereSlug('laravel')->first();

// Get recently active tags
$activeTags = Tag::whereRecentlyActive(7)->get(); // Active in last 7 days

// Get tags with high growth
$growingTags = Tag::whereHighGrowth(7)->get(); // Growing in last 7 days

// Calculate trending scores
Tag::calculateAllTrendingScores();

// Or calculate for a specific tag
$tag->calculateTrendingScore();

// Get tag activity summary
$summary = $tag->getActivitySummary();
// Returns: total_count, trending_score, daily_activity, weekly_activity,
// monthly_activity, weekly_growth_rate, last_activity

// Manual count management (usually handled automatically)
$tag->incrementCount();  // Increment usage count
$tag->decrementCount();  // Decrement usage count
$tag->updateCount();     // Recalculate count from actual usage

// Get activity metrics
$recentActivity = $tag->getRecentActivityCount(7);  // Count in last 7 days
$growthRate = $tag->getGrowthRate(7);              // Growth percentage

Configurable Trending Scores

The trending score calculation is fully configurable. You can adjust weights, time periods, and bonuses to match your application's needs:

'trending' => [
    'weights' => [
        'volume' => 0.25,
        'recency' => 0.30,
        'velocity' => 0.25,
        'freshness' => 0.20,
    ],

    'time_periods' => [
        'daily' => 1,
        'weekly' => 7,
        'monthly' => 30,
        'velocity_comparison' => 14,
    ],

    'scoring' => [
        'volume_normalization' => 50,
        'freshness_decay' => 2,
        'velocity_multiplier' => 50,
    ],

    'momentum_bonuses' => [
        'daily_activity' => 1.1,
        'weekly_threshold_40' => 1.15,
        'weekly_threshold_60' => 1.2,
    ],
],

Artisan Commands

Calculate trending scores for all tags:

php artisan tags:calculate-trending

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

ritechoice23/taggable 适用场景与选型建议

ritechoice23/taggable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「metadata」 「blog」 「search」 「cms」 「taxonomy」 「analytics」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ritechoice23/taggable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ritechoice23/taggable 我们能提供哪些服务?
定制开发 / 二次开发

基于 ritechoice23/taggable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 65
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 39
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-09