Tag Model (Spatie Tags)
The application uses Spatie's Tags package (spatie/laravel-tags) for tagging functionality. This provides a robust tagging system with multilingual support and polymorphic relationships.
Package Information
- Package:
spatie/laravel-tags - Model:
Spatie\Tags\Tag - Trait:
Spatie\Tags\HasTags
Table Structure
The tags table contains the following columns:
id- Primary keyname- JSON field containing tag names in different localesslug- JSON field containing tag slugs in different localestype- Optional tag type for categorizationorder_column- Integer for custom orderingcreated_at- Timestampupdated_at- Timestamp
The taggables pivot table contains:
tag_id- Foreign key to tags tabletaggable_id- ID of the tagged modeltaggable_type- Class name of the tagged model
Configuration
The package is configured in config/tags.php:
return [
'tag_model' => Spatie\Tags\Tag::class,
'taggable' => [
'table_name' => 'taggables',
'morph_name' => 'taggable',
]
];Usage Examples
Creating Tags
use Spatie\Tags\Tag;
// Create a simple tag
$tag = Tag::create(['name' => 'OTC']);
// Create a multilingual tag
$tag = Tag::create([
'name' => [
'en' => 'Over the Counter',
'es' => 'Sin Receta'
],
'slug' => [
'en' => 'over-the-counter',
'es' => 'sin-receta'
],
'type' => 'product_category'
]);Finding Tags
// Get all tags
$tags = Tag::all();
// Get tags by type
$productTags = Tag::ofType('product_category')->get();
// Find or create a tag
$tag = Tag::findOrCreate('OTC');Using Tags with Models
Models that use tags should include the HasTags trait:
use Spatie\Tags\HasTags;
class Product extends Model
{
use HasTags;
// ... rest of the model
}Then you can use tags like this:
// Attach tags to a model
$product->attachTag('OTC');
$product->attachTags(['OTC', 'Pain Relief', 'Fever']);
// Detach tags
$product->detachTag('OTC');
$product->detachTags(['OTC', 'Pain Relief']);
// Sync tags (replace all existing tags)
$product->syncTags(['OTC', 'Pain Relief']);
// Check if model has tag
if ($product->hasTag('OTC')) {
// Do something
}
// Get all tags for a model
$tags = $product->tags;
// Get tags by type
$categoryTags = $product->tags()->ofType('category')->get();API Endpoints
The Tag model is accessible through the following API endpoints:
GET /api/tags- List all tagsPOST /api/tags- Create a new tagGET /api/tags/{id}- Get a specific tagPUT /api/tags/{id}- Update a tagDELETE /api/tags/{id}- Delete a tag
Tag Types
The application supports different tag types for categorization:
product_category- For categorizing productsorder_status- For order status tagsmember_type- For member categorizationcarrier_specific- For carrier-specific tags
Localization
Tags support multiple languages through the JSON name and slug fields. The current locale is determined by the application's locale setting.
// Set locale
app()->setLocale('es');
// Get localized name
$tagName = $tag->name; // Returns Spanish name if availableAvailable Methods
The Spatie Tags package provides many useful methods:
// Tag creation and finding
Tag::findOrCreate('tag-name');
Tag::findFromString('tag-name');
Tag::getWithType('type-name');
// Tag relationships
$tag->taggables; // Get all tagged models
$tag->taggables()->where('taggable_type', Product::class)->get();
// Model tag operations
$model->attachTag('tag-name');
$model->attachTags(['tag1', 'tag2']);
$model->detachTag('tag-name');
$model->detachTags(['tag1', 'tag2']);
$model->syncTags(['tag1', 'tag2']);
$model->hasTag('tag-name');
$model->hasAnyTag(['tag1', 'tag2']);
$model->hasAllTags(['tag1', 'tag2']);Integration with Other Models
Currently, the following models use tags:
- Product - For categorizing products by type, category, etc.
- Order - For order status and categorization
- Member - For member type classification
- Carrier - For carrier-specific tags