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

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

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

hamedov/laravel-taxonomies

Composer 安装命令:

composer require hamedov/laravel-taxonomies

包简介

Assign categories, tags, etc, to eloquent models.

README 文档

README

Assign categories, tags, types, etc to your models or any taxonomy that can be used to classify models.

Installation

composer require hamedov/laravel-taxonomies

Publish config file

php artisan vendor:publish --provider="Hamedov\Taxonomies\TaxonomyServiceProvider" --tag="config"

Create taxonomies

use Hamedov\Taxonomies\Models\Taxonomy;

// Create new category
$category = Taxonomy::create([
  'title' => 'Electronics', // The only required field
  'description' => 'Electronics category description',
  'font_icon' => 'fa-laptop', // Enables you to assign font icons to your taxonomies
  'type' => 'category',
  'parent_id' => null, // Allow for sub categories/taxonomies
]);

// Or create a new tag
$tag = Taxonomy::create([
  'title' => 'Php', // The only required field
  'type' => 'tag',
]);

// Create any custom taxonomy
$ticket_class = Taxonomy::create([
  'title' => 'Class A',
  'description' => '',
  'font_icon' => 'fa-ticket',
  'type' => 'ticket_class',
]);

Assign image icons to taxonomies

  • The package uses spatie/laravel-medialibrary to allow you to assign image icons to taxonomies.
  • You can configure icons collection name and icon conversions in the config file, you can define as many conversions as you like.
  • The icon collection is a single file collection.
    'icon_collection_name' => 'taxonomy_icons',
      'icon_conversions' => [
      	'medium' => [120, 120], // Width, Height
      'small' => [90, 90],
      ],
    
    
  • Assign an image to taxonomy
    // You can pass any parameter that can be passed to addMedia method of medialibrary package
    $taxonomy->setIcon(request()->file('icon'));
    
  • Refer to Media library documentation for more info.

Translate taxonomies

  • You can also set translations for the title and description columns using spatie/laravel-translatable package.
  • You database server must support json columns for translations to work.
  • Refer to https://github.com/spatie/laravel-translatable for more information on how to translate these fields.

Manage model taxonomies

  • To be able to assign taxonomies to any of your models, the model must use the HasTaxonomies trait.

    use Hamedov/Taxonomies/Traits/HasTaxonomies;
    
    class Post extends Model {
      use HasTaxonomies;
      
    }
    
  • Assign taxonomies to models

    $post = Post::find(1);
    $taxonomy = Taxonomy::find(1);
    
    // Add taxonomy to post
    $post->addTaxonomy($taxonomy->id);
    
    // Or add many taxonomies at once
    $post->addTaxonomies([1, 2, 3]);
    
    // Set new taxonomies and remove existing
    // This will remove all post taxonomies of type category
    // And then adds the new specified taxonomies
    // To remove all old taxonomies of all types remove the second parameter
    $post->setTaxonomies([1, 2, 3], 'category');
    
    // Remove post taxonomy
    $post->removeTaxonomy($taxonomy_id = 1);
    
    // Remove post taxonomies of specific type
    $post->removeTaxonomies('tag', [5, 6, 7]);
    // Or remove all post tags
    $post->removeTaxonomies('tag');
    
    // Remove all post taxonomies
    $post->removeAllTaxonomies();
    
    // Get post taxonomies by type
    $categories = $post->taxonomies('category')->get();
    $tags = $post->taxonomies('tags')->get();
    
    // Or get all taxonomies at once
    $taxonomies = $post->taxonomies;
    
  • Available scopes

    // Scope taxonomies with specific type
    Taxonomy::type('category')->get();
    
    // Scope entries with a specific taxonomy
    $taxonomy = Taxonomy::find(1);
    $posts = Post::hasTaxonomy($taxonomy->id)->get();
    
    // Scope entries which have any of the specified taxonomies
    $posts = Post::hasAnyTaxonomy([1, 2, 3])->get();
    
    

Query models related to specific taxonomy

// We can only get one type of taxable models at once
// This is a limitation of many to many polymorphic relationships
$taxonomy = Taxonomy::find(1);
$taxonomy_posts = $taxonomy->taxables('App\Post')->get();
$taxonomy_products = $taxonomy->taxables('App\Product')->get();

// To get all models at once, we can do something like this
$taxonomy->load('taxes', 'taxes.taxable');
foreach($taxonomy->taxes as $tax)
{
  // $tax->taxable here is the post or product or any thing else.
  dd($tax->taxable);
}

Query related entries in pivot table

  • You can query entries related to specific taxonomy in pivot table
    $taxonomy = Taxonomy::find(1);
    $pivot_entries = $taxonomy->taxes()->where([
      // Filter by taxonomy_id, taxable_id, taxable_type
    ])->get();
    
    
  • You can also do the same from other model perspective
    $post = Post::find(1);
    $pivot_entries = $post->taxes()->where([
      // Filter by taxonomy_id, taxable_id, taxable_type
    ])->get();
    

License

Released under the Mit license, see LICENSE

统计信息

  • 总下载量: 1.43k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固