vildanbina / laravel-tags
为您的 Laravel 应用添加标签和可标记行为
Requires
- php: ^8.0
- laravel/framework: ^8.67|^9.0
- spatie/eloquent-sortable: ^3.10|^4.0
- spatie/laravel-package-tools: ^1.4
Requires (Dev)
- orchestra/testbench: ^6.13|^7.0
- phpunit/phpunit: ^9.5.2
- dev-main
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.1
- 1.4.0
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.1
This package is auto-updated.
Last update: 2024-08-29 05:37:39 UTC
README
为 Laravel 应用添加标签和可标记行为
此包为您提供了模型的可标记行为。安装包后,您只需将 HasTags
特性添加到 Eloquent 模型中即可使其可标记。
但我们不仅限于常规的标记功能。Laravel Tags 内置了电池。它默认支持 翻译标签、多标签类型 和 排序功能。
您可以在 https://spatie.be/docs/laravel-tags 上找到文档。
下面是一些代码示例
// apply HasTags trait to a model use Illuminate\Database\Eloquent\Model; use Spatie\Tags\HasTags; class NewsItem extends Model { use HasTags; // ... }
// create a model with some tags $newsItem = NewsItem::create([ 'name' => 'The Article Title', 'tags' => ['first tag', 'second tag'], //tags will be created if they don't exist ]); // attaching tags $newsItem->attachTag('third tag'); $newsItem->attachTag('third tag','some_type'); $newsItem->attachTags(['fourth tag', 'fifth tag']); $newsItem->attachTags(['fourth_tag','fifth_tag'],'some_type'); // detaching tags $newsItem->detachTags('third tag'); $newsItem->detachTags('third tag','some_type'); $newsItem->detachTags(['fourth tag', 'fifth tag']); $newsItem->detachTags(['fourth tag', 'fifth tag'],'some_type'); // get all tags of a model $newsItem->tags; // syncing tags $newsItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached // syncing tags with a type $newsItem->syncTagsWithType(['category 1', 'category 2'], 'categories'); $newsItem->syncTagsWithType(['topic 1', 'topic 2'], 'topics'); // retrieving tags with a type $newsItem->tagsWithType('categories'); $newsItem->tagsWithType('topics'); // retrieving models that have any of the given tags NewsItem::withAnyTags(['first tag', 'second tag'])->get(); // retrieve models that have all of the given tags NewsItem::withAllTags(['first tag', 'second tag'])->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 8 或更高版本,PHP 8 或更高版本,以及支持 json
字段和 MySQL 兼容函数的数据库。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-tags
包将自动注册。
您可以使用以下命令发布迁移
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"
在迁移发布后,您可以通过运行迁移来创建 tags
和 taggables
表
php artisan migrate
您可以选择使用以下命令发布配置文件
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-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://spatie.be/docs/laravel-tags/v4 上找到文档。
如果您在使用包时遇到困难?发现了错误?您是否有关于改进 laravel-tags
包的一般问题或建议?请随时在 GitHub 上创建问题 这里,我们将尽快解决。
如果您发现了有关安全性的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
测试
- 将
phpunit.xml.dist
复制到phpunit.xml
并填写您的数据库凭据。 - 运行
composer test
。
更新日志
请参阅 CHANGELOG 了解最近更改的信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全性
如果您发现了有关安全性的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
明信片软件
您可以自由使用此包,但如果它进入您的生产环境,我们非常感谢您从家乡寄给我们明信片,说明您正在使用我们的哪些包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
我们将所有收到的明信片发布在我们的公司网站上。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。