功能说明
使用最简便的方式,为你的数据模型提供强大「打标签」功能。
: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/

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInacookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionwwithSession_start()和stordoredAtain $ _session.2)

在PHP中,遍历会话数据可以通过以下步骤实现:1.使用session_start()启动会话。2.通过foreach循环遍历$_SESSION数组中的所有键值对。3.处理复杂数据结构时,使用is_array()或is_object()函数,并用print_r()输出详细信息。4.优化遍历时,可采用分页处理,避免一次性处理大量数据。这将帮助你在实际项目中更有效地管理和使用PHP会话数据。

会话通过服务器端的状态管理机制实现用户认证。1)会话创建并生成唯一ID,2)ID通过cookies传递,3)服务器存储并通过ID访问会话数据,4)实现用户认证和状态管理,提升应用安全性和用户体验。

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

PHPSession失效的原因包括配置错误、Cookie问题和Session过期。1.配置错误:检查并设置正确的session.save_path。2.Cookie问题:确保Cookie设置正确。3.Session过期:调整session.gc_maxlifetime值以延长会话时间。

在PHP中调试会话问题的方法包括:1.检查会话是否正确启动;2.验证会话ID的传递;3.检查会话数据的存储和读取;4.查看服务器配置。通过输出会话ID和数据、查看会话文件内容等方法,可以有效诊断和解决会话相关的问题。

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

在PHP中配置会话生命周期可以通过设置session.gc_maxlifetime和session.cookie_lifetime来实现。1)session.gc_maxlifetime控制服务器端会话数据的存活时间,2)session.cookie_lifetime控制客户端cookie的生命周期,设置为0时cookie在浏览器关闭时过期。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。