zing / laravel-eloquent-tags
Manage tags for Laravel eloquent
3.3.0
2024-03-18 14:37 UTC
Requires
- php: ^8.0
- illuminate/database: ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- mockery/mockery: ~1.3.3 || ^1.4.2
- orchestra/testbench: ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.3.3 || ^10.0
README
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.