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

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具