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.
composer require cartalyst/tags.php artisan vendor:publish --provider="Cartalyst\Tags\TagsServiceProvider".tags, tagged, and tag_slug_mappings tables.HasTags trait to any Eloquent model (e.g., Post):
use Cartalyst\Tags\HasTags;
class Post extends Model
{
use HasTags;
}
$post = Post::find(1);
$post->attachTags(['laravel', 'php']);
$post->syncTags(['news', 'trending']); // replaces existing tags
Post::taggedWith('laravel')->get();
Post::taggedWith(['laravel', 'api'], 'all')->get(); // all tags required
Post::taggedWithAny(['php', 'rust'])->get(); // any tag matches
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');
Tag::firstOrCreate() to avoid duplicates and ensure consistency:
$tag = Tag::firstOrCreate(['name' => 'Eloquent', 'slug' => 'eloquent']);
$post->attachTag($tag);
Laravel ≠ laravel). To normalize, override resolveTagName() in your model or use Tag::firstOrCreate() with slug.taggedWith() uses joins—ensure tags.slug, tagged.tag_id, and tagged.taggable_id are indexed.null context. Use consistent contexts to prevent tag collision across domains (e.g., blog posts vs. product SKUs).taggable model is soft-deleted, its associated tags remain intact. Use withoutTrashed() when querying.protected function resolveTagName($tag): string
{
return Str::slug($tag);
}
How can I help you explore Laravel packages today?