How spatie/laravel-tags works
What this project is (type + purpose)
This repo is a Laravel package/library (not a standalone app) that adds taggable behavior to any Eloquent model via a trait. It provides:
• A Tag Eloquent model and schema (tags + taggables)
• A HasTags trait that adds relationships + query scopes + attach/detach/sync APIs
• Optional features: translations (JSON fields), tag types, sorting/order_column, slugs
• Laravel integration via a Service Provider that publishes migrations and config
How to read the file tree (what maps to architecture)
Key architectural anchors in this repo:
Laravel integration / bootstrapping
• src/TagsServiceProvider.php
• Registers package with Laravel
• Enables publishing:
• database/migrations/create_tag_tables.php.stub
• config/tags.php
Domain model + behavior
• src/Tag.php
The core entity: represents a tag (name, slug, type, translations, order).
• src/HasTags.php
Trait applied to *host app models* to make them taggable (relationships, scopes, attach/detach/sync).
• src/HasSlug.php
Concern for slug generation (driven by config/tags.php slugger).
Persistence / schema
• database/migrations/create_tag_tables.php.stub
Defines:
• tags table (includes JSON fields for translatable values; order/sort column)
• taggables pivot table for polymorphic many-to-many (tag_id + taggable_id + taggable_type + type, etc.)
Configuration
• config/tags.php
• slugger function override (important “plug-in” point)
Quality / verification (useful to infer behavior but not “runtime components”)
• tests/** show supported behaviors and extension points (custom tag model, custom locale, translated attributes).
Context: where this package sits (inside a host Laravel app)
2) Container: the host app runtime containers (App, DB) and the package as an internal library module
3) Component: internal package components (ServiceProvider, Trait, Tag model, migrations/config)
You want to make it explicit that the “consumers” are other Eloquent models, not an HTTP client.
Host Laravel Application (external to this repo, but essential
• User code / Domain models (e.g., NewsItem extends Model)
• Adds use HasTags;
• Calls APIs like attachTag, syncTagsWithType, withAnyTags, etc.
• Laravel Framework
• Service container, Eloquent ORM, migrations, config, helpers
Spatie Tags Package (this repo)
• TagsServiceProvider
• Publishes migration + config
• (Potentially binds/extends tag model depending on config—tests indicate custom tag model support)
• HasTags trait
• Adds polymorphic many-to-many relationship to tags (via taggables)
• Provides query scopes (withAnyTags, withAllTags, withoutTags, and type-based variants)
• Provides commands: attach/detach/sync, translation helpers like tagsTranslated()
• Tag model
• Handles find-or-create, translations, slug, sorting/order
• HasSlug
• Slug generation based on config slugger (or default Str::slug)
Database (external but central)
• tags table
• “Tag definition” records (name/slug may be JSON if translatable)
• type + order_column for multi-type + sorting
• taggables table
• Pivot for polymorphic relation:
• tag_id
• taggable_id
• taggable_type
• (and possibly type to scope tags by type)
External dependencies worth labeling
• DB engine requirement: JSON support + MySQL-compatible JSON functions
Architectural patterns/principles to explicitly highlight
• Trait-based composition: HasTags is mixed into any Eloquent model.
• Polymorphic many-to-many: taggables enables tagging multiple model types without per-model pivot tables.
• Service Provider pattern: Laravel package bootstrapping + publishable assets.
• Config-driven behavior: slug generation is a pluggable function.
• Optional concerns layered on core:
• Translations via JSON fields (and helper methods)
• Types (categorizing tags)
• Sorting via order_column
Don’t overthink it: split into many components
• Separate HasTags vs Tag vs HasSlug vs ServiceProvider
• Separate tags table vs taggables pivot
• “Any Taggable Model(s)” as a polymorphic target
• Call out translation/type/sorting as feature modules (even if implemented inside the same classes)