clevel/laravel-tags-mongodb

为您的Laravel应用添加标签和可标记行为,适用于MongoDB

v1.0.4 2019-04-10 16:45 UTC

This package is not auto-updated.

Last update: 2024-09-19 18:30:22 UTC


README

以下是一些代码示例

//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"

要求

此包需要Laravel 5.7或更高版本,PHP 7.0或更高版本,以及支持json字段和函数的数据库,例如MySQL 5.7或更高版本。

安装

您可以通过composer安装此包

composer require clevel/laravel-tags

该包将自动注册自身。

php artisan vendor:publish --provider="Clevel\Tags\TagsServiceProvider" --tag="config"

这是已发布配置文件的包含内容

return [

    /*
     * The given function generates a URL friendly "slug" from the tag name property before saving it.
     */
    'slugger' => 'str_slug',
];