hamedov / laravel-taxonomies
将分类、标签等分配给Eloquent模型。
4.0.0
2023-09-04 22:52 UTC
Requires
- php: ^8.1
- illuminate/database: ^10.0
- illuminate/support: ^10.0
- spatie/laravel-medialibrary: ^10.0.0
- spatie/laravel-sluggable: ^3.5
- spatie/laravel-translatable: ^6.5
- staudenmeir/laravel-adjacency-list: ^1.13
README
将分类、标签、类型等分配给您的模型或任何可以用于分类模型的分类。
安装
composer require hamedov/laravel-taxonomies
发布配置文件
php artisan vendor:publish --provider="Hamedov\Taxonomies\TaxonomyServiceProvider" --tag="config"
创建分类
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',
]);
为分类分配图像图标
- 此包使用spatie/laravel-medialibrary来允许您为分类分配图像图标。
- 您可以在配置文件中配置图标集合名称和图标转换,您可以定义尽可能多的转换。
- 图标集合是一个单独的文件集合。
'icon_collection_name' => 'taxonomy_icons', 'icon_conversions' => [ 'medium' => [120, 120], // Width, Height 'small' => [90, 90], ],
- 为分类分配图像
// You can pass any parameter that can be passed to addMedia method of medialibrary package $taxonomy->setIcon(request()->file('icon'));
- 有关更多信息,请参阅媒体库文档。
翻译分类
- 您还可以使用spatie/laravel-translatable包设置
title
和description
列的翻译。 - 为了使翻译工作,您的数据库服务器必须支持json列。
- 有关如何翻译这些字段的更多信息,请参阅https://github.com/spatie/laravel-translatable。
管理模型分类
-
为了能够将分类分配给您的任何模型,该模型必须使用
HasTaxonomies
特质。use Hamedov/Taxonomies/Traits/HasTaxonomies; class Post extends Model { use HasTaxonomies; }
-
为模型分配分类
$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;
-
可用范围
// 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();
查询特定分类相关模型
// 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);
}
查询枢纽表中的相关条目
- 您可以在枢纽表中查询与特定分类相关的条目
$taxonomy = Taxonomy::find(1); $pivot_entries = $taxonomy->taxes()->where([ // Filter by taxonomy_id, taxable_id, taxable_type ])->get();
- 您也可以从其他模型的角度做同样的事情
$post = Post::find(1); $pivot_entries = $post->taxes()->where([ // Filter by taxonomy_id, taxable_id, taxable_type ])->get();
许可证
在Mit许可证下发布,请参阅LICENSE