idevin / laravel-tags

为您的 Laravel 应用添加标签和可标签化行为

dev-main 2021-11-15 12:13 UTC

This package is auto-updated.

Last update: 2024-09-15 18:53:51 UTC


README

Latest Version on Packagist MIT Licensed GitHub Workflow Status Total Downloads

此包为您的模型提供可标签化行为。安装包后,您只需将 HasTags 特性添加到 Eloquent 模型中即可使其可标签化。

但我们不仅仅提供了常见的标签功能。Laravel Tags 包含了电池式的功能。它默认支持 翻译标签多个标签类型排序功能

您可以在 https://spatie.be/docs/laravel-tags 上找到文档。

以下是一些代码示例

// 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');

// 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 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 https://spatie.be/opensource

支持我们

我们投入了大量资源来创建 最优秀开源包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄给我们明信片,并说明您正在使用我们哪个包。您可以在我们的 联系我们页面 找到我们的地址。我们将在 我们的虚拟明信片墙上 发布所有收到的明信片。

要求

此包需要 Laravel 8 或更高版本,PHP 8 或更高版本,以及支持 json 字段和 MySQL 兼容函数的数据库。

安装

您可以通过 composer 安装此包

composer require spatie/laravel-tags

包将自动注册自己。

您可以使用以下命令发布迁移

php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"

发布迁移后,您可以通过运行迁移来创建 tagstaggables

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 上 创建一个问题,我们会尽快解决。

如果您发现了关于安全的错误,请通过邮件 freek@spatie.be 而不是使用问题跟踪器。

测试

  1. .env.example 复制到 .env 并填写您的数据库凭据。
  2. 运行 composer test

更新日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现了任何与安全相关的错误,请通过邮件 freek@spatie.be 而不是使用问题跟踪器。

明信片软件

您可以使用这个软件包,但如果它进入了您的生产环境,我们非常感激您从家乡寄给我们一张明信片,并说明您正在使用我们哪个软件包。

我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。

我们将所有收到的明信片发布在我们的公司网站上

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅许可文件