hkp22/laravel-taggable

Laravel 模型标签扩展包。

1.0 2018-07-04 12:01 UTC

This package is auto-updated.

Last update: 2024-09-21 20:25:54 UTC


README

Laravel Eloquent 模型标签扩展包。

安装

您可以通过 composer 安装此包

composer require hkp22/laravel-taggable

用法

准备 Taggable 模型

use Hkp22\LaravelTaggable\Traits\Taggable;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Lesson extends Eloquent
{
    use Taggable;
}

注册包

app/config/app.php 中包含服务提供者

'providers' => [
    Hkp22\LaravelTaggable\TaggableServiceProvider::class,
],

标签 Eloquent 模型

例如:标记课程模型。

$lesson->tag(['laravel', 'php']);

或者

$tags = $tags = Tag::whereIn('slug', ['laravel', 'testing'])->get();

$lesson->tag($tags);

取消标签 Eloquent 模型

例如:取消标记课程模型。

// Un-tag single tag.
$lesson->untag(['laravel']);

// Un-tag all tags.
$lesson->untag();    

重新标记 Eloquent 模型

// Tag
$lesson->tag(['laravel', 'testing']);

// Re-tag
$lesson->retag(['laravel', 'postgres', 'redis']);

已标记实体总数。

$tag = Tag::first();

$tag->count;

获取带有任何给定标签的模型

$lessons = Lesson::withAnyTag(['php'])->get();

获取只带有给定标签的模型

$lessons = Lesson::withAllTags(['php', 'laravel', 'testing'])->get();