botdigit/laravel-taxonomies 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

botdigit/laravel-taxonomies

Composer 安装命令:

composer require botdigit/laravel-taxonomies

包简介

Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel ^ 7.0

README 文档

README

Simple, nestable Terms & Taxonomies (similar to WordPress) for Laravel 7.

Important Notice

This package is a work in progress, please use with care and feel free to report any issues or ideas you may have!

We've transferred this package to a new owner and therefor updated the namespaces to Botdigit\Taxonomies. The config file is now config/lecturize.php.

Installation

composer require botdigit/laravel-taxonomies

Require the package from your composer.json file

"require": {
    "botdigit/laravel-taxonomies": "dev-master"
}

and run $ composer update or both in one with $ composer require botdigit/laravel-taxonomies.

Next register the service provider and (optional) facade to your config/app.php file

'providers' => [
    // ...
    Cviebrock\EloquentSluggable\ServiceProvider::class,
    Botdigit\Taxonomies\TaxonomiesServiceProvider::class,
];

Configuration & Migration

$ php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
$ php artisan vendor:publish --provider="Botdigit\Taxonomies\TaxonomiesServiceProvider"

This will create a config/sluggable.php, a config/lecturize.php and a migration file, that you'll have to run like so:

$ php artisan migrate

Usage

First, add our HasTaxonomies trait to your model.

<?php namespace App\Models;

use Botdigit\Taxonomies\Traits\HasTaxonomies;

class Post extends Model
{
    use HasTaxonomies;

    // ...
}
?>
Add a Term
$model->addTerm('My Category', 'taxonomy')
Add multiple Terms
$model->addTerm(['Add','Multiple','Categories'], 'taxonomy')
Add a Term with optional parent (taxonomy) & order
$model->addTerm('My Category', 'taxonomy', 1, 2)
Get all Terms for a model by taxonomy
$model->getTerms('taxonomy')
Get a specific Term for a model by (optional) taxonomy
$model->getTerm('My Category', 'taxonomy')
Convenience method for getTerm()
$model->hasTerm($term, 'taxonomy')
Remove a Term from model by (optional) taxonomy
$model->removeTerm($term, 'taxonomy')
Remove all Terms from model
$model->removeAllTerms()
Scope models with multiple Terms
$model = Model::withTerms($terms, 'taxonomy')->get();
Scope models with one Term
$model = Model::withTerm($term, 'taxonomy')->get();

Example

Add categories to an Eloquent model

$post = Post::find(1);

$post->addTerm('My First Category', 'category');
$post->addTerm(['Category Two', 'Category Three'], 'category');

First fo 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. use Botdigit\Taxonomies\Models\Taxable; use Botdigit\Taxonomies\Models\Taxonomy; use Botdigit\Taxonomies\Models\Term;

$terms = Taxonomy::where('taxonomy', 'tags')->with('term')->get();

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->addTerm('Sheos', 'product_cat');. Afterwards you want to blog about that product and add that post to a post_cat called Shoes as well, using $product->addTerm('Sheos', 'post_cat');.

Normally you would have two entries now in your database, one like ['Sheos','product_cat'] and another ['Sheos','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.

License

Licensed under MIT license.

Author

Handcrafted by Tarun in India .

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固