1. Go to this page and download the library: Download masum/laravel-tagging library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
masum / laravel-tagging example snippets
use Masum\Tagging\Traits\Tagable;
class Equipment extends Model
{
use Tagable;
// Required: Define display name for the model
const TAGABLE = 'Equipment::Generic';
protected $fillable = ['name', 'description'];
}
use Masum\Tagging\Models\TagConfig;
TagConfig::create([
'model' => \App\Models\Equipment::class, // Full namespace auto_generate' => true,
]);
use Masum\Tagging\Models\Tag;
// Get all tags
Tag::all();
// Count tags
Tag::count(); // 3
// View tag details
$tag = Tag::first();
echo "Tag: {$tag->value}\n";
echo "Type: {$tag->taggable_type}\n";
echo "ID: {$tag->taggable_id}\n";
// Find equipment by tag
$equipment = Equipment::byTag('EQ-001')->first();
echo $equipment->name; // Cisco Router
// Search with pattern
Equipment::byTag('EQ-00%')->get(); // Returns all matching equipment
// Load all equipment with tags (prevents N+1 queries)
$allEquipment = Equipment::with('tag')->get();
foreach ($allEquipment as $eq) {
echo "{$eq->name} -> {$eq->tag}\n";
}
// Output:
// Cisco Router -> EQ-001
// TP-Link Switch -> EQ-002
// Dell Server -> EQ-003
// When you delete equipment, tags are automatically deleted
$eq = Equipment::find(1);
$tagValue = $eq->tag;
$eq->delete();
// Verify tag was deleted
Tag::where('value', $tagValue)->first(); // null
use App\Models\Equipment;
use Masum\Tagging\Models\Tag;
use Masum\Tagging\Models\TagConfig;
echo "=== Laravel Tagging Quick Test ===\n\n";
// Create config if not exists
if (!TagConfig::where('model', \App\Models\Equipment::class)->exists()) {
TagConfig::create([
'model' => \App\Models\Equipment::class,
'prefix' => 'EQ',
'separator' => '-',
'number_format' => 'sequential',
'auto_generate' => true,
]);
echo "✓ Config created\n";
}
// Create test equipment
$eq = Equipment::create(['name' => 'Test Item ' . time()]);
echo "✓ Equipment created: ID {$eq->id}\n";
// Check tag
if ($eq->tag) {
echo "✓ Tag generated: {$eq->tag}\n";
} else {
echo "✗ Tag NOT generated!\n";
}
// Verify in database
$tag = Tag::where('taggable_type', \App\Models\Equipment::class)
->where('taggable_id', $eq->id)
->first();
if ($tag) {
echo "✓ Tag in database: {$tag->value}\n";
} else {
echo "✗ Tag NOT in database!\n";
}
// Test search
$found = Equipment::byTag($eq->tag)->first();
if ($found && $found->id === $eq->id) {
echo "✓ Tag search working\n";
} else {
echo "✗ Tag search failed\n";
}
echo "\n=== All Tests Passed! ===\n";
$equipment = Equipment::find(1);
// Get tag value
echo $equipment->tag; // EQ-001
// Get tag model
$tagModel = $equipment->tag();
// Get tag configuration
$config = $equipment->tag_config;
// Ensure tag exists (generate if missing)
$equipment->ensureTag();
// Generate next tag without saving
$nextTag = $equipment->generateNextTag();
// Set custom tag
$equipment->tag = 'CUSTOM-001';
// Remove tag
$equipment->tag = null;
use Masum\Tagging\Models\Tag;
// Find model by tag value
$tag = Tag::where('value', 'EQ-001')->first();
$equipment = $tag->taggable;
// Get all tags for a model type
$equipmentTags = Tag::where('taggable_type', \App\Models\Equipment::class)->get();
class Brand extends Model
{
use Tagable;
const TAGABLE = 'Brand';
const TAG_LABEL = 'Brand: {name}'; // Variable interpolation
protected $fillable = ['name'];
}
use Masum\Tagging\Exceptions\{TagGenerationException, DuplicateTagException};
try {
$equipment = Equipment::create(['name' => 'Router']);
} catch (TagGenerationException $e) {
Log::error('Tag generation failed', ['error' => $e->getMessage()]);
// Assign manual tag or handle error
} catch (DuplicateTagException $e) {
// Handle duplicate tag scenario
}
class Equipment extends Model
{
use Tagable;
protected function generateSequentialTag(TagConfig $tagConfig): string
{
// Custom logic here
return parent::generateSequentialTag($tagConfig);
}
}
// ❌ Bad - Creates N+1 queries
$equipment = Equipment::all();
foreach ($equipment as $item) {
echo $item->tag; // Separate query each time!
}
// ✅ Good - Single query for all tags
$equipment = Equipment::with('tag')->get();
foreach ($equipment as $item) {
echo $item->tag; // Uses loaded relationship
}
// Atomic counter increment with SELECT FOR UPDATE
// Retries up to 3 times with exponential backoff
// Falls back to timestamp-based tags if all retries fail
// 1. Always validate inputs
$validated = $request->validate([
'name' => 'dleware
'routes' => [
'middleware' => ['api', 'auth:sanctum'],
],
// 3. Set APP_DEBUG=false in production
APP_DEBUG=false
// 4. Implement rate limiting
Route::middleware(['throttle:60,1'])->group(function () {
// Tag routes
});
use Masum\Tagging\Traits\Tagable;
class Equipment extends Model
{
use Tagable; // ✅ Trait must be present
const TAGABLE = 'Equipment::Generic'; // ✅ Required constant
}