- How do I install and set up Spatie’s Laravel Tags package in a Laravel project?
- Install via Composer with `composer require spatie/laravel-tags`, then add the `HasTags` trait to your Eloquent model. No additional configuration is needed for basic usage—migrations are auto-published and the package auto-registers. Run `php artisan migrate` to create the required tables.
- Which Laravel and PHP versions does this package support?
- The package supports Laravel 8 through 13 and requires PHP 8+. For new projects, PHP 8.2+ is recommended to align with Laravel’s latest LTS support. Always check the [release notes](https://github.com/spatie/laravel-tags/releases) for version-specific details.
- Can I use this package with databases other than MySQL or PostgreSQL?
- The package primarily supports MySQL 5.7+ and PostgreSQL with JSON extensions. SQLite is partially supported but may require manual adjustments. Older databases (e.g., MySQL 5.6) without JSON field support are not compatible without custom modifications.
- How do I query models based on tags (e.g., find posts with a specific tag)?
- Use Eloquent scopes like `withAnyTags(['tag1', 'tag2'])` to find models with *any* of the tags, or `withAllTags(['tag1', 'tag2'])` for models with *all* tags. For exclusion, use `withoutTags(['tag1'])`—these scopes are optimized for performance and reduce the need for raw SQL queries.
- Does this package support multilingual or translated tags?
- Yes, the package includes built-in support for translated tags via JSON fields. Tags can be stored with translations for multiple locales, but this may increase storage usage. For large-scale multilingual apps, consider caching translations or using Laravel’s built-in localization for non-tag content.
- How do I handle tag types (e.g., categorizing tags by purpose like 'category', 'topic')?
- Tag types are supported out of the box. Use methods like `attachTag('tag', 'type')` to assign a tag to a specific type. For bulk operations, use `syncTagsWithType(['tag1', 'tag2'], 'type')`. Document your tag type rules in API contracts to avoid accidental data loss during sync operations.
- What performance considerations should I keep in mind when scaling tag usage?
- Tagging creates pivot tables, which can impact performance at scale (e.g., millions of tags). Monitor query performance and consider indexing strategies, such as full-text search for tags using Laravel Scout. For high-traffic apps, test with sample data to establish a performance baseline.
- Can I retroactively add tags to existing models without downtime?
- Yes, you can add the `HasTags` trait to existing models and run a migration to create the necessary pivot tables. For large datasets, consider a phased rollout—start with non-critical models, then migrate others. Use Laravel’s model events (e.g., `retrieved`) to backfill tags if needed.
- Are there alternatives to this package for Laravel tagging?
- Alternatives include `laravel-tagging` (by Orchid) and `cviebrock/eloquent-sluggable` (for slug-based tagging). However, Spatie’s package stands out for its built-in support for translations, types, and sorting, along with seamless Laravel integration. Compare features like query flexibility and database support based on your project’s needs.
- How do I monitor or clean up orphaned tags (tags not attached to any model)?
- Use the `Tag` model’s `orphaned()` scope to find tags without attachments. For cleanup, manually delete orphaned tags or create a scheduled job to prune them periodically. Consider adding validation (e.g., minimum attachment count) to prevent orphaned tags from being created in the first place.