Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tags Laravel Package

cartalyst/tags

Cartalyst Tags adds flexible tagging to Laravel apps. Create, assign, and manage tags for any Eloquent model with polymorphic relations, including tag groups and retrieval helpers. Useful for categorization, search, and content organization across your app.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package via Composer: composer require cartalyst/tags.
  2. Publish the config and migration files: php artisan vendor:publish --provider="Cartalyst\Tags\TagsServiceProvider".
  3. Run migrations to create the required tags, tagged, and tag_slug_mappings tables.
  4. Add the HasTags trait to any Eloquent model (e.g., Post):
    use Cartalyst\Tags\HasTags;
    
    class Post extends Model
    {
        use HasTags;
    }
    
  5. Start tagging models with minimal code:
    $post = Post::find(1);
    $post->attachTags(['laravel', 'php']);
    

Implementation Patterns

  • Batch tag operations:
    $post->syncTags(['news', 'trending']); // replaces existing tags
    
  • Tag querying with scope-style methods:
    Post::taggedWith('laravel')->get();
    Post::taggedWith(['laravel', 'api'], 'all')->get(); // all tags required
    Post::taggedWithAny(['php', 'rust'])->get(); // any tag matches
    
  • Working with tag metadata: The package stores name, slug, and optional context (e.g., for separating blog tags from product tags). Use context() to isolate tag scopes:
    $post->attachTags(['frontend', 'backend'], 'category');
    
  • Reusing common tags: Use Tag::firstOrCreate() to avoid duplicates and ensure consistency:
    $tag = Tag::firstOrCreate(['name' => 'Eloquent', 'slug' => 'eloquent']);
    $post->attachTag($tag);
    

Gotchas and Tips

  • Case sensitivity: Tags are case-sensitive by default (e.g., Laravellaravel). To normalize, override resolveTagName() in your model or use Tag::firstOrCreate() with slug.
  • Performance on large datasets: taggedWith() uses joins—ensure tags.slug, tagged.tag_id, and tagged.taggable_id are indexed.
  • Context matters: If you omit context, all tags are stored under the default null context. Use consistent contexts to prevent tag collision across domains (e.g., blog posts vs. product SKUs).
  • Soft-deletes: Tags are not soft-deleted. If a taggable model is soft-deleted, its associated tags remain intact. Use withoutTrashed() when querying.
  • Extending tag behavior: Override methods in your model to customize tag resolution or add logic on tag changes:
    protected function resolveTagName($tag): string
    {
        return Str::slug($tag);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport