vendocrat/laravel-taxonomies
最新稳定版本:v1.0.5
Composer 安装命令:
composer require vendocrat/laravel-taxonomies
包简介
Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel.
README 文档
README
Laravel Taxonomies
Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel.
Installation
Require the package from your composer.json file
"require": { "lecturize/laravel-taxonomies": "^1.0" }
and run $ composer update or both in one with $ composer require lecturize/laravel-taxonomies.
Configuration & Migration
$ php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider" $ php artisan vendor:publish --provider="Lecturize\Taxonomies\TaxonomiesServiceProvider"
This will publish a config/sluggable.php, a config/lecturize.php and some migration files, that you'll have to run:
$ php artisan migrate
For migrations to be properly published ensure that you have added the directory database/migrations to the classmap in your projects composer.json.
Usage
First, add our HasCategories trait to your model.
<?php namespace App\Models; use Lecturize\Taxonomies\Contracts\CanHaveCategories; use Lecturize\Taxonomies\Traits\HasCategories; class Post extends Model implements CanHaveCategories { use HasCategories; // ... } ?>
Add a Term
$model->addCategory('My Category', 'blog_category')
Add multiple Terms
$model->addCategories(['Add','Multiple','Categories'], 'blog_category')
Add a Term with optional parent_id (taxonomy->id) & sort order
$model->addCategory('My Category', 'blog_category', 1, 2)
Get all Terms for a model by taxonomy
$model->getCategories('taxonomy')
Get a specific Term for a model by (optional) taxonomy
$model->getCategory('My Category', 'blog_category')
See if model has a given category within given taxonomy
$model->hasCategory('My Category', 'blog_category')
Remove a Term from model by (optional) taxonomy
$model->removeCategory('My Category', 'blog_category')
Remove (detach) all categories relations from model
$model->detachCategories()
Scope models with any of the given categories
$model = Model::categorizedIn(['Add','Multiple','Categories'], 'blog_category')->get();
Scope models with one category
$model = Model::categorized('My Category', 'blog_category')->get();
Helper functions
I've included a set of helper functions for your convenience, see src/helpers.php.
Example
Add categories to an Eloquent model
$post = Post::find(1); $post->addCategory('My First Category', 'blog_category'); $post->addCategories(['Category Two', 'Category Three'], 'blog_category');
First of all, this snippet will create three entries in your terms table, if they don't already exist:
- My First Category
- Category Two
- Category Three
Then it will create three entries in your taxonomies table, relating the terms with the given taxonomy "category".
And last it will relate the entries from your taxonomies table with your model (in this example a "Post" model) in your pivot table.
Why three tables?
Imagine you have a Taxonomy called post_cat and another one product_cat, the first categorises your blog posts, the second the products in your online shop. Now you add a product to a category (a term) called Shoes using $product->addCategory('Shoes', 'product_cat');. Afterwards you want to blog about that product and add that post to a post_cat called Shoes as well, using $product->addCategory('Shoes', 'post_cat');.
Normally you would have two entries now in your database, one like ['Shoes','product_cat'] and another ['Shoes','post_at']. Oops, now you recognize you misspelled Shoes, now you would have to change it twice, for each Taxonomy.
So I wanted to keep my Terms unique throughout my app, which is why I separated them from the Taxonomies and simply related them.
Changelog
- [2021-02-09] v1.0 Extended the database tables to support UUIDs (be sure to generate some on your existing models) and better customization. Quite some breaking changes throughout the whole package.
License
Licensed under MIT license.
Author
Handcrafted with love by Alexander Manfred Poellmann in Vienna & Rome.
vendocrat/laravel-taxonomies 适用场景与选型建议
vendocrat/laravel-taxonomies 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 78, 最近一次更新时间为 2015 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「categories」 「model」 「laravel」 「terms」 「taxonomies」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vendocrat/laravel-taxonomies 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vendocrat/laravel-taxonomies 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vendocrat/laravel-taxonomies 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Categories for NetCommons Plugin
PHP Interface for Babel Street Text Analytics
Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 74
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 79
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-30