功能说明
使用最简便的方式,为你的数据模型提供强大「打标签」功能。
: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 is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.