waavi/tagging

Laravel 5 Eloquent 模型的标签。

2.0.2 2016-10-23 18:16 UTC

This package is auto-updated.

Last update: 2024-09-14 00:29:27 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

简介

此包允许您轻松地向 Eloquent 模型添加标签。受 handy cviebrock/eloquent-taggable 包的启发。

WAAVI 是一家位于西班牙马德里的网络开发工作室。您可以在 waavi.com 上了解更多关于我们的信息

Laravel 兼容性

安装

通过 composer 需要

composer require waavi/tagging ^2.0

或者手动编辑您的 composer.json 文件

"require": {
    "waavi/tagging": "^2.0"
}

将以下条目添加到 app/config.php 中 providers 数组的末尾

Waavi\Tagging\TaggingServiceProvider::class,

如果您不使用 Eloquent-SluggableWaavi\Translation,您还需要添加

Cviebrock\EloquentSluggable\SluggableServiceProvider::class,
Waavi\Translation\TranslationServiceProvider::class,

发布配置文件并运行迁移

php artisan vendor:publish --provider="Waavi\Tagging\TaggingServiceProvider"
php artisan migrate

现在您可以使用您的设置编辑 config/tagging.php。

从版本 1.x 更新

2.x 版本与 1.x 版本不兼容。您需要完全删除 v1、删除迁移,然后从头开始重新安装。

配置

您可以在 config/tagging 中找到配置文件。

return [
    // Remove all the tag relations on model delete
    'on_delete_cascade' => true,
    // If you want your tag names to be translatable using waavi/translation, set to true.
    'translatable'      => false,
    // All tag names will be trimed and normalized using this function:
    'normalizer'        => 'mb_strtolower',
];

用法

您的模型应该实现 Taggable 的接口并使用其 trait

use Waavi\Tagging\Traits\Taggable;

class Post extends Model
{
    use Taggable;
}

向现有模型添加标签而不删除现有标签

// Tag with a comma separated list of tags:
$model->tag('apple,orange');

// Tag with an array of tags:
$model->tag(['apple', 'orange']);

在现有模型中替换给定的现有标签

// Tag with a comma separated list of tags:
$model->retag('apple,orange');

// Tag with an array of tags:
$model->retag(['apple', 'orange']);

从现有模型中删除标签

// Remove tags with a comma separated list:
$model->untag('apple,orange');

// Remove tags with an array of tags:
$model->untag(['apple', 'orange']);

从现有模型中删除所有标签

$model->detag();

获取标签

// As comma separated list:
$model->tagNames;

// As array ['apple', 'orange']:
$model->tagArray;

// Get a list of all of the tags ever applied to any model of the same class: ['apple', 'orange', 'strawberry']
$model->availableTags();

按标签获取

// Get entries that have ALL of the given tags:
$model->withAllTags('apple, orange');
$model->withAllTags(['apple', 'orange']);

// Get entries that have ANY of the given tags:
$model->withAnyTags('apple, orange');
$model->withAnyTags(['apple', 'orange']);

// Get a list of all of the tags ever applied to any model of the same class: ['apple', 'orange', 'strawberry']
$model->availableTags();