laravel-tags maintained by sourcedopen
Description
A laravel package for tags
Author
Last update
2026/03/17 19:48
(dev-main)
License
Downloads
0
Tags
A Laravel package for adding tags to Eloquent models using polymorphic relationships.
Installation
You can install the package via Composer:
composer require sourcedopen/laravel-tags
The package will automatically register its service provider.
Configuration
Publish and run the migrations:
php artisan vendor:publish --tag="tags-migrations"
php artisan migrate
Usage
Adding Tags to a Model
Use the HasTags trait in any model you want to be taggable:
use Illuminate\Database\Eloquent\Model;
use SourcedOpen\Tags\Traits\HasTags;
class Post extends Model
{
use HasTags;
}
Creating Tags
use SourcedOpen\Tags\Models\Tag;
$tag = Tag::create([
'name' => 'Laravel',
'color' => '#FF2D20', // Optional hex color code
]);
Attaching Tags
// Attach a single tag
$post->attachTags($tag->id);
// Attach multiple tags
$post->attachTags([$tag1->id, $tag2->id]);
Detaching Tags
// Detach a single tag
$post->detachTags($tag->id);
// Detach multiple tags
$post->detachTags([$tag1->id, $tag2->id]);
Syncing Tags
// Sync tags (removes all existing and attaches the provided ones)
$post->syncTags([$tag1->id, $tag2->id]);
Retrieving Tags
// Get all tags for a model
$tags = $post->tags;
// Query with tags
$posts = Post::whereHas('tags', function ($query) {
$query->where('name', 'Laravel');
})->get();
Testing
composer test
Or run Pest directly:
./vendor/bin/pest
Code Style
This package uses Laravel Pint for code styling:
./vendor/bin/pint
License
The MIT License (MIT). Please see License File for more information.