zing/laravel-eloquent-tags

管理Laravel Eloquent标签

3.3.0 2024-03-18 14:37 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

要求PHP 8.0+

使用Composer要求Laravel Eloquent Tags

composer require zing/laravel-eloquent-tags

用法

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

许可证

Laravel Eloquent Tags是一个开源软件,许可协议为MIT许可证