usamamuneerchaudhary / laravel-tagify
Attach, detach, and query tags on Eloquent models using a polymorphic relationship.
Package info
github.com/usamamuneerchaudhary/laravel-tagify
pkg:composer/usamamuneerchaudhary/laravel-tagify
Fund package maintenance!
Requires
- php: ^8.3
- illuminate/contracts: ^11.0|^12.0|^13.0
- illuminate/database: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
- phpstan/phpstan: ^2.0
- usamamuneerchaudhary/laravel-slipway: ^1.8
Suggests
- ext-intl: Improves transliteration quality when normalizing non-Latin tag names.
Provides
None
Conflicts
None
Replaces
None
README
Tagify is a simple Tagging Package for Laravel
Using this package, you can tag, untag, or retag any Eloquent model, and listen for attach and detach events.
- Laravel 11, 12, and 13
- PHP 8.3+
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Usamamuneerchaudhary\LaravelTagify\Taggable;
class Lesson extends Model
{
use Taggable;
}
Installation
You can install the package via composer:
composer require usamamuneerchaudhary/laravel-tagify
The service provider is discovered automatically.
Run Migrations
Once the package is installed, you can run migrations:
php artisan migrate
Usage & Some basic functions
Add Tags to a Model
$tags = ['Laravel', 'PHP'];
$model->tag($tags);
Untag a tag
$model->untag(['Laravel']);
Untag all tags
$model->untag();
Re-tag a tag to a model
$model->retag(['Eloquent', 'mysql']);
Get all tags
$model->tags;
You can also use scopes to get models associated with tags:
App\Models\Lesson::withAllTags(['Laravel', 'php']); // models that have every given tag
App\Models\Lesson::withAnyTag(['Laravel', 'php', 'python']); // models that have at least one of the given tags
App\Models\Lesson::hasTags(['Laravel', 'php']); // models that have at least one of the given tags
Tag names are normalized to slugs before they are stored or queried, so Laravel and laravel refer to the same tag.
Events
TagAttached and TagDetached are dispatched after tags are actually attached or detached, and only after the surrounding database transaction commits:
use Usamamuneerchaudhary\LaravelTagify\Events\TagAttached;
use Usamamuneerchaudhary\LaravelTagify\Events\TagDetached;
Event::listen(TagAttached::class, function (TagAttached $event) {
$event->taggable;
$event->tags;
});
Event::listen(TagDetached::class, function (TagDetached $event) {
$event->taggable;
$event->tags;
});
Tests
composer test
Security
If you discover any security related issues, please email hello@usamamuneer.me instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.