承接 zaichaopan/taggable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zaichaopan/taggable

Composer 安装命令:

composer require zaichaopan/taggable

包简介

A package to make eloquent model taggable

关键字:

README 文档

README

This Package is used to make eloquent model taggable. It can be used in laravel 5.5 or higher.

Installation

composer require zaichaopan/taggable

Usage

  • Add tags and taggables table

After you install the package, run migration command to add tags and taggables table

php artisan migrate

The schemas of these two tables

// tags table
Schema::create('tags', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
    $table->string('slug')->unique();
    $table->timestamps();
});
// taggables table
Schema::create('taggables', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('tag_id');
    $table->foreign('tag_id')->references('id')->on('tags');
    $table->morphs('taggable');
    $table->timestamps();
});

This packages also provides Tag and Taggable models.

// To add a create tag
use  Zaichaopan\Taggable\Models\Tag;

Tag::create(['name' => 'laravel']);

// To get all the tag names
$names = Tag::getNames();

Note:

Please make sure tag name is unique. After you create or update a tag, its slug value will be automatically added or updated based on its name value.

  • Add hasTags trait to the model you want to tag.

Let's say you have Activity model and you can give it tag.

//
use Zaichaopan\Taggable\Traits\HasTags;

class Activity extends Model
{
    use HasTags;
}

The hasTags trait provides the following methods to the model which uses it

tags:

It is used to get all the tags of the model:

$tags = $activity->tags;

tag:

/**
 *
 * @param string|array ...$tagNames
 */
public function tag(...$tagNames): void
$activity->tag('outdoor');

// or
$activity->tag('outdoor', 'sports');

// or
$activity->tag(['outdoor', 'sports']);

Note:

The tag name provided must be valid (exists in the tags table). If a tag name is invalid, it will be ignored. If a tag has already been given to a model, it will not be given again.

reTag:

/**
 *
 * @param string|array ...$tagNames
 */
public function reTag(...$tagNames): void

It is used to update a model's tag. After calling this method on a model, all its old tags will be moved and only the new tag will remain. If the tag name provided is invalid, it will be ignored. If all the provided tag names are invalid, the model will not be retagged. It will still keep its old tags.

$activity->tag('outdoor');

// the tag of the activity will be sports
$activity->reTag('sports');

// or
$activity->reTag(['outdoor', 'sports']);

unTag:

/**
 *
 * @param string|array ...$tagNames
 */
public function unTag(...$tagNames): void

It is used to remove a tag or multiple tags from a model. If a given tag name is invalid, it will be ignore. If all the given tag names are invalid, none of the model's tags will be removed.

$activity->unTag('outdoor');

// or
$activity->unTag('outdoor', 'sports');

// or
$activity->unTag(['outdoor', 'sports']);

unTagAll:

public function unTagAll(): void

It is used to removed all the tags of the model.

$activity->unTagAll();

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固