zing/laravel-eloquent-tags

Manage tags for Laravel eloquent

3.3.0 2024-03-18 14:37 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

Requires PHP 8.0+

Require Laravel Eloquent Tags using Composer:

composer require zing/laravel-eloquent-tags

Usage

use Zing\LaravelEloquentTags\Tests\Models\Product;
use Zing\LaravelEloquentTags\Tag;

$product = Product::query()->first();
// Add tag(s) to model
$product->attachTag("tag");
$product->attachTags([
    "tag",
    Tag::query()->first()
]);
// Remove tag(s) from model
$product->detachTag("tag");
$product->detachTags([
    "tag",
    Tag::query()->first()
]);
// Reset tags of model
$product->syncTags([
    "tag",
    Tag::query()->first()
]);
// Get tags of model
$product->tags;
// Eager load tags
$products = Product::query()->with('tags')->withCount('tags')->get();
$products->each(function (Product $product){
    $product->tags->dump();
    $product->tags_count;
});
// Query by tag
Product::query()->withAnyTags(['tag', 'github'])->exists(); // true
Product::query()->withAllTags(['tag', 'github'])->exists(); // false

License

Laravel Eloquent Tags is an open-sourced software licensed under the MIT license.