Laravel Taggable 为你的模型添加打标签功能
功能说明
使用最简便的方式,为你的数据模型提供强大「打标签」功能。
:gift: 项目地址: https://github.com/summerblue/laravel-taggable
本项目修改于 rtconner/laravel-tagging项目,增加了一下功能:
- 标签名唯一;
- 增加 etrepat/baum依赖,让标签支持无限级别标签嵌套;
- 中文 slug 拼音自动生成支持,感谢超哥的 overtrue/pinyin;
- 提供完整的测试用例,保证代码质量。
注意: 本项目只支持 5.1 LTS
:heart: 此项目由 The EST Group团队的 @Summer维护。
无限级别标签嵌套
集成 etrepat/baum让标签具备从属关系。
$root = Tag::create(['name' => 'Root']);// 创建子标签$child1 = $root->children()->create(['name' => 'Child1']);$child = Tag::create(['name' => 'Child2']);$child->makeChildOf($root);// 批量构建树$tagTree = [ 'name' => 'RootTag', 'children' => [ ['name' => 'L1Child1', 'children' => [ ['name' => 'L2Child1'], ['name' => 'L2Child1'], ['name' => 'L2Child1'], ] ], ['name' => 'L1Child2'], ['name' => 'L1Child3'], ]];Tag::buildTree($tagTree);
更多关联操作请查看: etrepat/baum。
标签名称规则说明
- 标签名里的特殊符号和空格会被 -替代;
- 智能标签 slug 生成,会生成 name 对应的中文拼音 slug ,如: 标签-> biao-qian,拼音一样的时候会被加上随机值;
标签名清理使用: $normalize_string = EstGroupe\Taggable\Util::tagName($name)。
Tag::create(['标签名']);// name: 标签名// slug: biao-qian-mingTag::create(['表签名']);// name: 表签名// slug: biao-qian-ming-3243 (后面 3243 为随机,解决拼音冲突)Tag::create(['标签 名']);// name: 标签-名// slug: biao-qian-mingTag::create(['标签!名']);// name: 标签-名// slug: biao-qian-ming
安装说明:
安装
composer require estgroupe/laravel-taggable "5.1.*"
安装和执行迁移
在 config/app.php的 providers数组中加入:
'providers' => array( \EstGroupe\Taggable\Providers\TaggingServiceProvider::class,);
php artisan vendor:publish --provider="EstGroupe\Taggable\Providers\TaggingServiceProvider"php artisan migrate
请仔细阅读 config/tagging.php文件。
创建 Tag.php
不是必须的,不过建议你创建自己项目专属的 Tag.php 文件。
<?php namespace App\Models;use EstGroupe\Taggable\Model\Tag as TaggableTag;class Tag extends TaggableTag{ // Model code go here}
修改 config/tagging.php文件中:
'tag_model'=>'\App\Models\Tag',
加入 Taggable Trait
<?php namespace App\Models;use Illuminate\Database\Eloquent\Model;use EstGroupe\Taggable\Taggable;class Article extends \Illuminate\Database\Eloquent\Model { use Taggable;}
「标签状态」标示
Taggable能跟踪模型是否打过标签的状态:
// `no`$article->is_tagged// `yes`$article->tag('Tag1');$article->is_tagged;// `no`$article->unTag();$article->is_tagged// This is fast$taggedArticles = Article::where('is_tagged', 'yes')->get()
首先你需要修改 config/tagging.php文件中:
'is_tagged_label_enable' => true,
然后在你的模型的数据库创建脚本里加上:
<?phpuse Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateArticlesTable extends Migration { public function up() { Schema::create('weibo_statuses', function(Blueprint $table) { $table->increments('id'); ... // Add this line $table->enum('is_tagged', array('yes', 'no'))->default('no'); ... $table->timestamps(); }); }}
「推荐标签」标示
方便你实现「推荐标签」功能,只需要把 suggest字段标示为 true:
$tag = EstGroupe\Taggable\Model\Tag::where('slug', '=', 'blog')->first();$tag->suggest = true;$tag->save();
即可以用以下方法读取:
$suggestedTags = EstGroupe\Taggable\Model\Tag::suggested()->get();
重写 Util 类?
大部分的通用操作都发生在 Util 类,你想获取更多的定制权力,请创建自己的 Util 类,并注册服务提供者:
namespace My\Project\Providers;use EstGroupe\Taggable\Providers\TaggingServiceProvider as ServiceProvider;use EstGroupe\Taggable\Contracts\TaggingUtility;class TaggingServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton(TaggingUtility::class, function () { return new MyNewUtilClass; }); }}
然后在
注意 MyNewUtilClass必须实现 EstGroupe\Taggable\Contracts\TaggingUtility接口。
使用范例
$article = Article::with('tags')->first(); // eager load// 获取所有标签foreach($article->tags as $tag) { echo $tag->name . ' with url slug of ' . $tag->slug;}// 打标签$article->tag('Gardening'); // attach the tag$article->tag('Gardening, Floral'); // attach the tag$article->tag(['Gardening', 'Floral']); // attach the tag$article->tag('Gardening', 'Floral'); // attach the tag// 批量通过 tag ids 打标签$article->tagWithTagIds([1,2,3]);// 去掉标签$article->untag('Cooking'); // remove Cooking tag$article->untag(); // remove all tags// 重打标签$article->retag(['Fruit', 'Fish']); // delete current tags and save new tags$article->retag('Fruit', 'Fish');$article->retag('Fruit, Fish');$tagged = $article->tagged; // return Collection of rows tagged to article$tags = $article->tags; // return Collection the actual tags (is slower than using tagged)// 获取绑定的标签名称数组$article->tagNames(); // get array of related tag names// 获取打了「任意」标签的 Article 对象Article::withAnyTag('Gardening, Cooking')->get(); // fetch articles with any tag listedArticle::withAnyTag(['Gardening','Cooking'])->get(); // different syntax, same result as aboveArticle::withAnyTag('Gardening','Cooking')->get(); // different syntax, same result as above// 获取打了「全包含」标签的 Article 对象Article::withAllTags('Gardening, Cooking')->get(); // only fetch articles with all the tagsArticle::withAllTags(['Gardening', 'Cooking'])->get();Article::withAllTags('Gardening', 'Cooking')->get();EstGroupe\Taggable\Model\Tag::where('count', '>', 2)->get(); // return all tags used more than twiceArticle::existingTags(); // return collection of all existing tags on any articles
如果你,即可使用以下标签读取功能:
// 通过 slug 获取标签Tag::byTagSlug('biao-qian-ming')->first();// 通过名字获取标签Tag::byTagName('标签名')->first();// 通过名字数组获取标签数组Tag::byTagNames(['标签名', '标签2', '标签3'])->first();// 通过 Tag ids 数组获取标签数组Tag::byTagIds([1,2,3])->first();// 通过名字数组获取 ID 数组$ids = Tag::idsByNames(['标签名', '标签2', '标签3'])->all();// [1,2,3]
标签事件
Taggabletrait 提供以下两个事件:
EstGroupe\Taggable\Events\TagAdded;EstGroupe\Taggable\Events\TagRemoved;
监听标签事件:
\Event::listen(EstGroupe\Taggable\Events\TagAdded::class, function($article){ \Log::debug($article->title . ' was tagged');});
单元测试
基本用例测试请见: tests/CommonUsageTest.php。
运行测试:
composer installvendor/bin/phpunit --verbose
Thanks
- Special Thanks to: Robert Conner - http://smartersoftware.net
- overtrue/pinyin
- etrepat/baum
- Made with love by The EST Group - http://estgroupe.com/

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

PHP中有四种主要错误类型:1.Notice:最轻微,不会中断程序,如访问未定义变量;2.Warning:比Notice严重,不会终止程序,如包含不存在文件;3.FatalError:最严重,会终止程序,如调用不存在函数;4.ParseError:语法错误,会阻止程序执行,如忘记添加结束标签。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

箭头函数在PHP7.4中引入,是短闭包的简化形式。1)它们使用=>运算符定义,省略function和use关键字。2)箭头函数自动捕获当前作用域变量,无需use关键字。3)它们常用于回调函数和短小计算,提高代码简洁性和可读性。
