sourcedopen / tags
A laravel package for tags
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/sourcedopen/tags
Requires
- php: ^8.1
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.27
- orchestra/testbench: ^9.0|^10.0
- pestphp/pest: ^4.3
README
A Laravel package for adding tags to Eloquent models using polymorphic relationships.
Installation
You can install the package via Composer:
composer require sourcedopen/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.