Skip to content

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 key
  • name - JSON field containing tag names in different locales
  • slug - JSON field containing tag slugs in different locales
  • type - Optional tag type for categorization
  • order_column - Integer for custom ordering
  • created_at - Timestamp
  • updated_at - Timestamp

The taggables pivot table contains:

  • tag_id - Foreign key to tags table
  • taggable_id - ID of the tagged model
  • taggable_type - Class name of the tagged model

Configuration

The package is configured in config/tags.php:

php
return [
    'tag_model' => Spatie\Tags\Tag::class,
    'taggable' => [
        'table_name' => 'taggables',
        'morph_name' => 'taggable',
    ]
];

Usage Examples

Creating Tags

php
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

php
// 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:

php
use Spatie\Tags\HasTags;

class Product extends Model
{
    use HasTags;
    
    // ... rest of the model
}

Then you can use tags like this:

php
// 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 tags
  • POST /api/tags - Create a new tag
  • GET /api/tags/{id} - Get a specific tag
  • PUT /api/tags/{id} - Update a tag
  • DELETE /api/tags/{id} - Delete a tag

Tag Types

The application supports different tag types for categorization:

  • product_category - For categorizing products
  • order_status - For order status tags
  • member_type - For member categorization
  • carrier_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.

php
// Set locale
app()->setLocale('es');

// Get localized name
$tagName = $tag->name; // Returns Spanish name if available

Available Methods

The Spatie Tags package provides many useful methods:

php
// 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