priblo / laravel-has-tags
以性能为中心的模型标签特性
v1.0.0-beta.5
2018-09-17 00:49 UTC
Requires
- php: >=7.1.0
- illuminate/database: 5.5.*|5.6.*|5.7.*
- illuminate/support: 5.5.*|5.6.*|5.7.*
Requires (Dev)
- orchestra/database: 3.6.*
- orchestra/testbench: 3.6.*
- phpunit/phpunit: ~7.0
This package is auto-updated.
Last update: 2024-09-30 01:40:33 UTC
README
以性能为中心的模型标签特性
特性使用
将特性添加到任何您需要成为可标签化模型的Eloquent模型中
Priblo\LaravelHasTags\Traits\HasTags;
用法
$Post = new Post(); // Tag Model // (Duplicates are automatically handled) $Post->tag(['tag1', 'tag2', 'tag3', 'tag1']); // Tag Model with type $Post->tag(['tag1', 'tag2', 'tag3', 'tag1'], 'hashtag'); // Count ALL tags attached to the model $Post->tags->count() // Count ONLY tags with type $Post->tagsWithType('hashtag')->count() // Retag model // Will remove previous tags and add the new ones // Type is optional $Post->reTag(['tag5', 'tag6'],'hashtag'); // Removes ALL tags without type from Model $Post->untag() // Removes ONLY tags with the specified type from Model $Post->untag('hashtag') // Removes ALL tags from model $Post->untagALL() // Retrieves all tagged models with the specified tag and type (optional) $posts = Post::withAnyTag(['tag1'],'hashtag')->get(); // Retrieves all related models according to Tag and type (optional) // Returns a collection of Priblo\LaravelHasTags\Models\Related $relatedCollection = Post::getRelatedByTag('tag1', 'hashtag));
关于类型的说明
每个标签都有一个类型,如果未指定,则默认为NULL。一个空类型仍然是一个类型,这意味着使用 untag()
方法只会移除 type === NULL
的标签。这是一个设计选择。
配置
缓存
缓存需要支持标签的驱动程序。文件和数据库将不会工作,建议使用redis。请确保在配置中禁用缓存或使用数组驱动程序进行本地开发。
在 has-tags.php 配置文件中,您可以选择启用/禁用缓存并设置缓存过期时间。
默认启用缓存
安装
要求:Laravel >=5.5 和 PHP 7.1
Composer
composer require priblo/laravel-has-tags
Laravel
此包支持自动发现。
如果您已禁用它,可以通过将 ./config/app.php 中的 'providers' 数组添加此包来安装此包。
Priblo\LaravelHasTags\LaravelServiceProvider::class,
然后运行
php artisan vendor:publish --provider="Priblo\LaravelHasTags\LaravelServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Priblo\LaravelHasTags\LaravelServiceProvider" --tag="config"
然后迁移
php artisan migrate
为什么还需要另一个标签特性?
在 Priblo,我们找不到一个适合Laravel的合适的标签特性。每一个都因为某种原因而不够好。主要是在性能方面。标签是Priblo的重要组成部分,我们需要把它们做好。选择 装饰器模式,我们注重缓存和性能。我们考虑向其他项目贡献,但没有一个是符合我们的设计理念的。因此,这是我们的实现,希望它对其他人也有用。