desdim / laravel-tags-uuid
使用uuid将标签和可标签行为添加到您的Laravel应用程序中
Requires
- php: 8.*
- laravel/framework: ^8.67|^9.0|^10.0
- nesbot/carbon: ^2.63
- spatie/eloquent-sortable: ^3.10|^4.0
- spatie/laravel-package-tools: ^1.4
- spatie/laravel-translatable: ^4.6|^5.0|^6.0
Requires (Dev)
- orchestra/testbench: ^6.13|^7.0
- pestphp/pest: ^1.22
- phpunit/phpunit: ^9.5.2
This package is not auto-updated.
Last update: 2024-09-26 14:17:20 UTC
README
使用UUID代替id
此包为您的模型提供可标签行为。安装包后,您只需将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(); // retrieve models that don't have any of the given tags NewsItem::withoutTags(['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发送邮件,而不是使用问题跟踪器。
Postcardware
您可以自由使用此软件包,但如果它进入您的生产环境,我们非常感激您从家乡寄给我们一张明信片,注明您正在使用我们的哪个(些)软件包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
我们将所有收到的明信片发布在我们的公司网站上。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。