blamodex / laravel-tags
A simple, Laravel-ready tagging package for rapid prototyping and lightweight use cases.
Installs: 19
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/blamodex/laravel-tags
Requires
- php: ^8.2
- illuminate/config: ^12.0
- illuminate/database: ^12.0
- illuminate/queue: ^12.0
- illuminate/support: ^12.0
Requires (Dev)
- blamodex/laravel-ai-agents: ^1.0
- larastan/larastan: ^3.0
- orchestra/testbench: ^10.0
- phpunit/php-code-coverage: *
- phpunit/phpunit: ^11.5.3
- squizlabs/php_codesniffer: 4.x-dev
README
A simple, Laravel-ready tagging package for rapid prototyping and lightweight use cases. Add flexible tagging functionality to any Eloquent model using polymorphic relationships.
Table of Contents
๐ Features
- Attach tagging functionality to any model using traits
- Polymorphic support for multiple taggable and tagger models
- Tag groups for categorization and organization
- Automatic slug generation for tags and tag groups
- Soft deletes on all entities
- Clean service layer pattern
- UUID support for distributed systems
๐ฆ Installation
Install the package with Composer:
composer require blamodex/laravel-tags
Run the migrations:
php artisan migrate
๐งฉ Usage
1. Making Models Taggable
Add the Taggable trait and implement the TaggableInterface to models that can be tagged:
use Blamodex\Tags\Traits\Taggable; use Blamodex\Tags\Contracts\TaggableInterface; class Post extends Model implements TaggableInterface { use Taggable; }
2. Making Models Taggers
Add the Tagger trait and implement the TaggerInterface to models that can create and own tags:
use Blamodex\Tags\Traits\Tagger; use Blamodex\Tags\Contracts\TaggerInterface; class User extends Model implements TaggerInterface { use Tagger; }
3. Working with Tags
Create a tag
$user = User::find(1); $tag = $user->createTag('Laravel');
Find a tag
$tag = $user->findTag('Laravel');
Find or create a tag
$tag = $user->findOrCreateTag('Laravel');
Check if user has a tag
if ($user->hasTag('Laravel')) { // Tag exists }
Get all tags owned by a user
$tags = $user->ownedTags();
Delete a tag
$user->deleteTag('Laravel');
4. Working with Tag Groups
Tag groups allow you to organize tags into categories.
Create a tag group
$user = User::find(1); $group = $user->createTagGroup('Programming Languages');
Find a tag group
$group = $user->findTagGroupByName('Programming Languages'); // or by slug $group = $user->findTagGroupBySlug('programming-languages');
Assign a tag to a group
$tag = $user->findTag('Laravel'); $group = $user->findTagGroupByName('Frameworks'); $user->assignTagToGroup($tag, $group);
Get all tags in a group
$tags = $user->getTagsByGroup($group);
Delete a tag group
$user->deleteTagGroup('Programming Languages');
5. Attaching Tags to Models
Once you have taggable models and tags, you can attach them:
Attach a tag to a model
$post = Post::find(1); $tag = Tag::findByName('Laravel'); $post->attachTag($tag);
Check if a model has a tag
if ($post->hasTag($tag)) { // Post is tagged }
Get all tags on a model
$tags = $post->tags();
Detach a tag from a model
$post->detachTag($tag);
Detach all tags from a model
$post->detachAllTags();
Sync tags (detach all existing and attach new ones)
$tags = [ Tag::findByName('Laravel'), Tag::findByName('PHP'), ]; $post->syncTags($tags);
๐งช Testing
This package uses Orchestra Testbench and PHPUnit.
Run tests:
composer test
Check code style:
composer lint
Check code style and fix:
composer lint:fix
Run static analysis:
composer analyze
Check coverage (with Xdebug):
composer test:coverage
๐ Project Structure
src/
โโโ Models/
โ โโโ Tag.php
โ โโโ TagGroup.php
โ โโโ TaggablePivot.php
โโโ Services/
โ โโโ TaggerService.php
โ โโโ TaggableService.php
โโโ Traits/
โ โโโ Taggable.php
โ โโโ Tagger.php
โ โโโ Sluggable.php
โโโ Contracts/
โ โโโ TaggableInterface.php
โ โโโ TaggerInterface.php
โ โโโ SluggableInterface.php
โโโ Events/
โ โโโ TagCreated.php
โ โโโ TagUpdated.php
โ โโโ TagDeleted.php
โ โโโ TaggablePivotCreated.php
โ โโโ TaggablePivotDeleted.php
โโโ Validators/
โ โโโ TagValidator.php
โโโ database/
โโโ migrations/
โโโ create_tag_groups_table.php
โโโ create_tags_table.php
โโโ create_taggable_pivots_table.php
tests/
โโโ Unit/
โโโ Fixtures/
โโโ TestCase.php
๐ค Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
๐ Changelog
Please see CHANGELOG.md for recent changes.
๐ License
MIT ยฉ Blamodex
For more information, see the LICENSE file.