Laravel的优雅标签包

v0.0.5 2023-12-08 15:15 UTC

This package is auto-updated.

Last update: 2024-09-09 07:01:59 UTC


README

Taggy是一个Laravel包,为Eloquent模型提供标签功能。通过这个简单灵活的包,您可以轻松地为模型添加和管理标签。

功能

  • 标签:将标签与您的Eloquent模型关联。
  • 可标签化特性:使用可标签化特性轻松使模型可标签化。

安装

要安装Taggy,只需运行

composer require polashmahmud/taggy

然后,迁移您的数据库

php artisan migrate

用法

标签模型

要使模型可标签化,请在模型上使用 Polashmahmud\Taggy\Taggable 特性

use Polashmahmud\Taggy\Taggable;

class Post extends Model
{
    use Taggable;
}

创建标签

要创建标签,请在 Polashmahmud\Taggy\Models\Tag 模型上使用 create() 方法

use Polashmahmud\Taggy\Models\Tag;

$tag = Tag::create([
    'name' => 'Laravel',
    'slug' => 'laravel',
]);

标签项

要标记一个项,请在模型上使用 tag() 方法,传入标签名称或缩略名作为数组

$post = Post::find(1);

$post->tag(['Laravel']);
$post->tag(['Laravel', 'PHP']);

您也可以传入标签

$post = Post::find(1);
$tag = Tag::find(1);

$post->tag($tag);

取消标记项

要从一个项中删除标签,请在模型上使用 untag() 方法

$post = Post::find(1);

$post->untag('Laravel');
$post->untag(['Laravel', 'PHP']);

从项中删除所有标签

$post = Post::find(1);

$post->untag();

重新标记项

要从一个项中删除所有标签并添加新标签,请在模型上使用 retag() 方法

$post = Post::find(1);

$post->retag(['Laravel']);
$post->retag(['Laravel', 'PHP']);

标签范围

Taggy提供了一些有用的查询范围,用于处理标签

// Get all posts tagged with 'Laravel' and 'PHP'
$posts = Post::withAnyTag(['laravel', 'php'])->get();

// Get common tags for all posts
$posts = Post::withAllTags(['Laravel', 'PHP'])->get();

// GreaterThanEqual and LessThanEqual scopes
$tags = Tag::usedGreaterThanEqual(10)->get();
$tags = Tag::usedGreaterThen(10)->get();
$tags = Tag::usedLessThanEqual(10)->get();
$tags = Tag::usedLessThan(10)->get();

贡献

感谢您考虑为Taggy做出贡献!请参阅CONTRIBUTING以获取详细信息。

许可

Taggy是开源软件,许可协议为MIT许可