qmagix / laravel-tagging
使用PHP特性扩展Laravel Eloquent模型以支持标签。模型可以被标记为可标签化。基于rtconner的工作,以兼容MySQL 5.6。
2.2.1
2016-09-12 21:50 UTC
Requires
- php: >=5.5.0
- illuminate/database: >= 5.0
- illuminate/support: >= 5.0
Requires (Dev)
- mockery/mockery: ~0.9
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~4.0
- vlucas/phpdotenv: ~2.0
README
此包是从rtconner同名包中派生的,以支持数据库字符串长度问题,该问题与MySQL 5.6不兼容。
此包不打算以任何方式处理javascript或html。此包只处理数据库存储和读写。
在标签中使用的字符没有实际限制。它使用slug转换来确定两个标签是否相同(“sugar-free”和“Sugar Free”被视为相同的标签)。标签显示名称通过Str::title()处理。
Laravel/Lumen 5 文档
Laravel 4 文档
Composer 安装(用于Laravel 5.3/Lumen 5)
composer require qmagix/laravel-tagging
安装并运行迁移
服务提供者不会在每次页面加载时加载,因此它不会减慢您的应用程序。
'providers' => array( \Qmagix\Tagging\Providers\TaggingServiceProvider::class, );
php artisan vendor:publish --provider="Qmagix\Tagging\Providers\TaggingServiceProvider"
php artisan migrate
Lumen 5 安装
Lumen没有vendor:publish命令,因此您需要将提供的迁移和配置文件创建或复制到相应的目录中。
在 app\bootstrap\app.php
// Add this line in your config section $app->configure('tagging'); // Add this line in your service provider section $app->register(Qmagix\Tagging\Providers\LumenTaggingServiceProvider::class);
完成这两个步骤后,您可以使用您首选的设置编辑config/tagging.php。
设置您的模型
class Article extends \Illuminate\Database\Eloquent\Model { use \Qmagix\Tagging\Taggable; }
快速示例用法
$article = Article::with('tagged')->first(); // eager load foreach($article->tags as $tag) { echo $tag->name . ' with url slug of ' . $tag->slug; } $article->tag('Gardening'); // attach the tag $article->untag('Cooking'); // remove Cooking tag $article->untag(); // remove all tags $article->retag(array('Fruit', 'Fish')); // delete current tags and save new tags $article->tagNames(); // get array of related tag names Article::withAnyTag(['Gardening','Cooking'])->get(); // fetch articles with any tag listed Article::withAllTags(['Gardening', 'Cooking'])->get(); // only fetch articles with all the tags Qmagix\Tagging\Model\Tag::where('count', '>', 2)->get(); // return all tags used more than twice Article::existingTags(); // return collection of all existing tags on any articles
标签组
您可以使用以下Artisan命令创建组
php artisan tagging:create-group MyTagGroup
为标签设置标签组
$tag->setGroup('MyTagGroup');
获取特定组中的所有标签
Tag::inGroup('MyTagGroup')->get()
检查标签是否在组中
$tag->isInGroup('MyTagGroup');
配置
更多文档
将Laravel 4升级到5
此库将完整的模型类名存储到数据库中。当您升级Laravel并给模型添加命名空间时,您需要更新数据库中存储的记录。或者,您可以在模型类中覆盖Model::$morphClass以与数据库中存储的字符串相匹配。
鸣谢
- Robert Conner - http://smartersoftware.net