Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/zingimmick/laravel-eloquent-tags
- Owner: zingimmick
- License: mit
- Created: 2020-10-16T09:05:27.000Z (over 4 years ago)
- Default Branch: 3.x
- Last Pushed: 2024-08-13T16:18:47.000Z (5 months ago)
- Last Synced: 2024-10-11T21:55:52.162Z (3 months ago)
- Topics: eloquent, laravel, tags
- Language: PHP
- Homepage:
- Size: 92.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Eloquent Tags
> **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).