hamedrajabpour/laravel-tags

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

1.0.8 2024-08-29 08:39 UTC

This package is auto-updated.

Last update: 2024-09-29 08:46:46 UTC


README

Latest Version on Packagist MIT Licensed GitHub Workflow Status Total Downloads

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

此软件包与已移除翻译和缩略名的Spatie之间的主要区别

以下是一些代码示例

// 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->detachTag('third tag');
$newsItem->detachTag('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();


// using tag types
$tag = Tag::findOrCreate('tag 1', 'my type');


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

要求

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

安装

您可以通过composer安装此软件包

composer require hamedrajabpour/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"

测试

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

许可证

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