Taggy is a Laravel package that provides tagging functionality for your Eloquent models. Easily add and manage tags for your models with this simple and flexible package.
- Tagging: Associate tags with your Eloquent models.
- Taggable Trait: Easily make your models taggable using the Taggable trait.
To install Taggy, simply run:
composer require polashmahmud/taggy
Then, migrate your database:
php artisan migrate
To make a model taggable, use the Polashmahmud\Taggy\Taggable
trait on the model:
use Polashmahmud\Taggy\Taggable;
class Post extends Model
{
use Taggable;
}
To create a tag, use the create()
method on the Polashmahmud\Taggy\Models\Tag
model:
use Polashmahmud\Taggy\Models\Tag;
$tag = Tag::create([
'name' => 'Laravel',
'slug' => 'laravel',
]);
To tag an item, use the tag()
method on the model, passing in the tag name or slug as a array:
$post = Post::find(1);
$post->tag(['Laravel']);
$post->tag(['Laravel', 'PHP']);
also you can pass tag:
$post = Post::find(1);
$tag = Tag::find(1);
$post->tag($tag);
To remove a tag from an item, use the untag()
method on the model:
$post = Post::find(1);
$post->untag('Laravel');
$post->untag(['Laravel', 'PHP']);
Remove all tags from an item:
$post = Post::find(1);
$post->untag();
To remove all tags from an item and add new tags, use the retag()
method on the model:
$post = Post::find(1);
$post->retag(['Laravel']);
$post->retag(['Laravel', 'PHP']);
Taggy provides a few useful query scopes for working with tags:
// Get all posts tagged with 'Laravel' and 'PHP'
$posts = Post::withAnyTag(['laravel', 'php'])->get();
// Get common tags for all posts
$posts = Post::withAllTags(['Laravel', 'PHP'])->get();
// GreaterThanEqual and LessThanEqual scopes
$tags = Tag::usedGreaterThanEqual(10)->get();
$tags = Tag::usedGreaterThen(10)->get();
$tags = Tag::usedLessThanEqual(10)->get();
$tags = Tag::usedLessThan(10)->get();
Thank you for considering contributing to Taggy! Please see CONTRIBUTING for details.
Taggy is open-sourced software licensed under the MIT license.