app-ark / laravel-tags
为您的Laravel应用添加标签和可标签化行为
Requires
- php: ^5.6|^7.0|^7.1|^7.2|^7.3
- app-ark/eloquent-sortable: >=0.0.1
- laravel/framework: >=5.0
Requires (Dev)
- orchestra/testbench: ~3.8.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-11 21:54:17 UTC
README
本软件包为您的模型提供可标签化行为。安装包后,您只需将HasTags
特质添加到Eloquent模型,即可使其可标签化。
但我们并没有止步于每个包中都有的常规标签功能。Laravel Tags内置了多种功能。它默认支持翻译标签、多种标签类型和排序功能。
您可以在https://docs.spatie.be/laravel-tags/v2上找到文档。
以下是一些代码示例
//create a model with some tags $newsItem = NewsItem::create([ 'name' => 'testModel', 'tags' => ['tag', 'tag2'], //tags will be created if they don't exist ]); //attaching tags $newsItem->attachTag('tag3'); $newsItem->attachTags(['tag4', 'tag5']); //detaching tags $newsItem->detachTags('tag3'); $newsItem->detachTags(['tag4', 'tag5']); //syncing tags $newsItem->syncTags(['tag1', 'tag2']); // all other tags on this model will be detached //syncing tags with a type $newsItem->syncTagsWithType(['tag1', 'tag2'], 'typeA'); $newsItem->syncTagsWithType(['tag1', 'tag2'], 'typeB'); //retrieving tags with a type $newsItem->tagsWithType('typeA'); $newsItem->tagsWithType('typeB'); //retrieving models that have any of the given tags NewsItem::withAnyTags(['tag1', 'tag2'])->get(); //retrieve models that have all of the given tags NewsItem::withAllTags(['tag1', 'tag2'])->get(); //translating a tag $tag = Tag::findOrCreate('my tag'); $tag->setTranslation('name', 'fr', 'mon tag'); $tag->setTranslation('name', 'nl', 'mijn tag'); $tag->save(); //getting translations $tag->translate('name'); //returns my name $tag->translate('name', 'fr'); //returns mon tag (optional locale param) //convenient translations through taggable models $newsItem->tagsTranslated();// returns tags with slug_translated and name_translated properties $newsItem->tagsTranslated('fr');// returns tags with slug_translated and name_translated properties set for specified locale //using tag types $tag = Tag::findOrCreate('tag 1', 'my type'); //tags have slugs $tag = Tag::findOrCreate('yet another tag'); $tag->slug; //returns "yet-another-tag" //tags are sortable $tag = Tag::findOrCreate('my tag'); $tag->order_column; //returns 1 $tag2 = Tag::findOrCreate('another tag'); $tag2->order_column; //returns 2 //manipulating the order of tags $tag->swapOrder($anotherTag);
Spatie是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述这里。
要求
本软件包需要Laravel 5.8或更高版本、PHP 7.2或更高版本以及支持json
字段和函数(如MySQL 5.7或更高版本)的数据库。
安装
您可以通过composer安装该包
composer require spatie/laravel-tags
包将自动注册自己。
您可以使用以下命令发布迁移
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="migrations"
发布迁移后,您可以通过运行迁移来创建tags
和taggables
表
php artisan migrate
您还可以选择使用以下命令发布配置文件
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="config"
这是已发布配置文件的内容
return [ /* * The given function generates a URL friendly "slug" from the tag name property before saving it. * Defaults to Str::slug (https://laravel.net.cn/docs/5.8/helpers#method-str-slug) */ 'slugger' => null, ];
文档
您可以在https://docs.spatie.be/laravel-tags/v2上找到文档。
在使用软件包时遇到困难?发现了错误?您对改进laravel-tags
软件包有一般性疑问或建议?请随时在GitHub上创建问题,我们将尽快解决。
如果您发现有关安全性的错误,请发送电子邮件至freek@spatie.be,而不是使用问题跟踪器。
测试
- 将
.env.example
复制到.env
并填写您的数据库凭据。 - 运行
composer test
。
变更日志
请参阅CHANGELOG以了解最近更改的详细信息。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的错误,请发送电子邮件至freek@spatie.be,而不是使用问题跟踪器。
明信片软件
您可以自由使用此软件包,但如果它进入您的生产环境,我们非常感谢您从您的家乡寄给我们一张明信片,说明您正在使用我们的哪些软件包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将把所有收到的明信片发布在我们的公司网站上。
致谢
支持我们
Spatie是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述这里。
您的业务是否依赖于我们的贡献?请通过 Patreon 支持我们。所有的承诺都将用于分配人力资源进行维护和开发新酷炫的功能。
许可证
MIT 许可证 (MIT)。更多信息请参阅 许可证文件。