Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zingimmick/laravel-eloquent-tags

Manage tags for Laravel eloquent
https://github.com/zingimmick/laravel-eloquent-tags

eloquent laravel tags

Last synced: 3 months ago
JSON representation

Manage tags for Laravel eloquent

Awesome Lists containing this project

README

        

# Laravel Eloquent Tags


Latest Stable Version
Total Downloads
Latest Unstable Version
License

> **Requires [PHP 8.0+](https://php.net/releases/)**

Require Laravel Eloquent Tags using [Composer](https://getcomposer.org):

```bash
composer require zing/laravel-eloquent-tags
```

## Usage

```php
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](LICENSE).